Focus video player for keyboard shortcuts
Auto
- 720p
- 540p
- 360p
1.00x
cc
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
How to use the While loop.
Exercise files
Download this lesson’s related exercise files.
While Loops58.8 KB While Loops - Solution
59.2 KB
Quick reference
While Loops
In this video we'll learn about while loops.
When to use
You can often use any sort of loop you like, while loops are pretty easy to use though.
Instructions
The general format of a while loop is:
while (condition) {
//do something;
}
Be sure to set a variable as a counter, and increment the counter inside the loop so you don't get an infinite loop!
Hints & tips
- While loops are used to loop "while" something is true.
- 00:04 In this video, we're going to start to talk about loops,
- 00:07 specifically while loops.
- 00:08 And now, loops are just incredibly important to all computer programming.
- 00:12 It doesn't matter what programming language you're using.
- 00:15 You're always gonna use loops.
- 00:16 They are probably just as important as if else statements, and
- 00:19 that you're gonna use them all the time.
- 00:20 There's a couple of different loops we're gonna look at.
- 00:22 We're gonna look at the while loop and for loop.
- 00:24 In this video, we're gonna focus on the while loop.
- 00:25 The difference between the two is sort of trivial, and
- 00:28 in many instances, you can use any kind of loop you want but
- 00:31 I find that programmers tend to gravitate towards a certain loop.
- 00:35 Me personally, I like to use while loop so I tend to use them all the time.
- 00:39 I could easily use a for loop, instead of a while loop, it's just
- 00:42 a personal preference, so you'll find the one that you like to use the most and
- 00:45 that's the one you'll use for most circumstances.
- 00:48 Now there are some times when you just have to use a while loop or a for loop or
- 00:51 whatever, but it's not all that rare.
- 00:52 So anyway, a while loop looks like, while condition do something.
- 01:01 Now, this looks an awful lot like an if statement.
- 01:03 And in fact, if we change the word while to if, we would have an if statement.
- 01:07 So if you know how to use an if statement, you're already sort of familiar with this.
- 01:10 That's probably why I like to use the while loop, it's just familiar.
- 01:13 So here, we can use our comparison operators.
- 01:16 We can do all kinds of stuff.
- 01:18 And in fact, let's just create one.
- 01:19 So to use a while loop, first we have to set a counter.
- 01:22 And I'm just gonna call it x.
- 01:23 I'm gonna set it equal to zero.
- 01:25 And I'm gonna say while x is less than ten, while this is true, do something.
- 01:31 And let's echo out, your number is x.
- 01:37 And I'm gonna put a line break here.
- 01:38 So it put this on, it's online, okay.
- 01:41 We also need to increment our variable, and to do that,
- 01:44 we use our increment operator that we already learned, and that's just x++.
- 01:49 Now, why do we need to do that?
- 01:51 Well, what happens is, this executes and it checks this condition.
- 01:55 As long as this is true, it'll do this next line.
- 01:57 If we didn't have this here, x would stay zero forever.
- 02:01 And it would loop back round, and it would say zero is less than ten.
- 02:05 It would echo this out.
- 02:06 It would loop back around, zero is less than ten.
- 02:09 Echo loop, zero less than ten, echo loop.
- 02:11 And you get what's called an infinite loop.
- 02:13 It would just keep going forever and ever and ever.
- 02:15 And when we print out, your number is zero a zillion times on the screen, and
- 02:19 your program would crash.
- 02:20 To this day, I forget to do my increment operator on here every now and then, and
- 02:25 my programs crash.
- 02:26 It's just one of the pitfalls of using the while loop.
- 02:29 But it's gonna happen to you, don't worry about it.
- 02:32 It's not a big deal.
- 02:32 You just restart your program and you go I forgot my increment operator.
- 02:36 So let's save this, come up here, and hit reload and
- 02:39 we can just see your number is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
- 02:42 And then it stops.
- 02:43 Why does it do that?
- 02:44 Once it hits 9, 9 is less than 10, so it prints out your number is 9.
- 02:49 Then it increments, and the 9 becomes 10 and then we loop again and
- 02:54 10 is not less than 10, so the whole thing stops.
- 02:57 This thing continues to loop while this is true, but
- 03:00 10 is less than 10 that's false so the whole things stops.
- 03:04 So that's pretty much it, that's a while loop.
- 03:06 There are some variations on the while loop specifically the do while loop,
- 03:11 and you can look up how to do a do while loop.
- 03:13 I've never actually used one.
- 03:15 This while loop will get you through most of a looping that you'll ever need to do.
- 03:18 In the next video, we're gonna talk about for loops.
- 03:21 It's very similar to a while loop, just slightly different and
- 03:24 you'll see all about that in the next video.
Lesson notes are only available for subscribers.