Locked lesson.
About this lesson
Using while loops in Ruby.
Exercise files
Download this lesson’s related exercise files.
While Loops.docx59 KB While Loops - Solution.docx
59 KB
Quick reference
While Loops
You can loop through things with a While loop.
When to use
Anytime you need to loop through things, you can use a while loop.
Instructions
To create a while loop:
counter = 0
while counter < 10 do
#loop code
counter += 1
end
Hints & tips
- While loops are an easy way to loop
- Don't forget to increment your counter or you'll get an infinite loop!
- 00:04 In this video, I want to talk about while loops.
- 00:06 Now a loop is another one of those just very fundamental programming things and
- 00:11 you're going to use them a lot.
- 00:12 And a loop just lets you continue doing something over, and over, and
- 00:16 over again until some condition gets met.
- 00:19 So we're going to be using our comparison operators, our conditional stuff
- 00:23 that we've learned in the past videos and we're going to use that with our loops.
- 00:26 Now there's lots of different loops.
- 00:27 And using a particular loop, sometimes you need to use a while loop, or
- 00:32 a for loop, or this loop or that loop.
- 00:35 Most of the time though, you can use any type of loop and
- 00:38 it just sort of becomes a personal preference of which one you like to use.
- 00:41 You'll find yourself using a certain one more often than others just because you're
- 00:45 more comfortable with it.
- 00:46 I personally like while loops, I always use them.
- 00:49 I don't know why, I just tend to like them, but a while loop looks like this.
- 00:53 So we have while, and then some condition, and then do,
- 00:57 and then do something and then end.
- 01:01 So it looks a lot like an if statement, right?
- 01:04 If condition, do something, and then end.
- 01:06 And maybe that's why I like it, it reminds me of an if statement.
- 01:10 So that's basically how they look, so let's create one.
- 01:13 Before you create a while loop, you usually need to create a counter.
- 01:16 So let's create a counter, and I'm just going to call it number and
- 01:19 set it equal to 0.
- 01:21 So we say while our number is, let's say less than 10,
- 01:25 that's our condition, number is less than 10.
- 01:28 As long as the number is less than 10, keep on looping.
- 01:31 Then we type in do, and
- 01:32 you can see our program automatically slaps the end on there.
- 01:35 So what do we want to do inside of our loop?
- 01:37 Well, let's just print out the number to the screen.
- 01:39 So let's go puts and then let's call number and
- 01:42 now we need to increment our number so we can use our assignment operator.
- 01:47 Remember that +=, += 1.
- 01:49 So why do we do that?
- 01:50 Well, if we don't increment our number, what'll happen is our number starts out as
- 01:55 0, and then while 0 is less than 10, which is true, it'll print out 0.
- 02:02 And it'll loop back around and it'll start over, and it'll say, is 0 less than 10?
- 02:07 Well, yes, it is, so it'll print out 0, loop back around, do it again, and again,
- 02:10 and again, and again.
- 02:11 And that's what's called an infinite loop.
- 02:13 It'll just keep printing out 0s to the screen forever.
- 02:16 And your program will eventually crash.
- 02:18 So we need to increment it every time in order to make sure that doesn't happen.
- 02:22 Don't worry, you will write an infinite loop, I still, to this day,
- 02:25 forget to put my increment thing on there and crash my programs.
- 02:29 So anyway, save this, let's run it.
- 02:32 So we just get 1, 2, 3, 4, 5, 7, 8, 9, 10.
- 02:35 Why, because it started with 0 is less than 10.
- 02:38 So it increments it and assigns it and puts it out.
- 02:41 So we have 1 and then 1 is less than 10.
- 02:44 So then it prints out 2, 3 is less than 10.
- 02:47 Finally it gets to 9, it says is 9 less than 10, yes.
- 02:50 It increments it to 10, prints it out to the screen,
- 02:53 comes back around again, it says 10, is 10 less than 10?
- 02:57 No it's not.
- 02:57 So it ends.
- 02:58 So that's a while loop.
- 03:00 In fact, I know I'm going to regret this because it's going to crash my program.
- 03:04 But let's just run this for fun.
- 03:06 Let's create our infinite loop.
- 03:07 We didn't put our increment operator on there and
- 03:09 we see boom 0, 0, 0, it just keeps going.
- 03:12 It will never stop and I actually have to close this thing.
- 03:14 Close and open a new one, new terminal.
- 03:19 Okay, yes, yes and let it reboot because it's still crashing on us.
- 03:23 I may actually have to restart this entire thing but those are while loops,
- 03:26 watch out for the infinite loop.
- 03:27 Always remember to increment.
- 03:29 In the next video, we'll look at until loops,
- 03:32 hopefully if my terminal stops crashing.
- 03:35 All right, so that's all for this video
Lesson notes are only available for subscribers.