Locked lesson.
About this lesson
In this lesson, we introduce Loops and explain how While and Do While work.
Exercise files
Download this lesson’s related exercise files.
36 - Loops While Loops.docx57.7 KB 36 - Loops While Loops SOLUTION.docx
58.8 KB
Quick reference
Loops: While Loops
While loops allow you to loop through code WHILE a condition is true.
When to use
Use them to loop through code.
Instructions
While Loop:
int x = 1;
while(x <= 10)
{
Console.WriteLine(x);
x++;
}
Do While Loop:
int x = 1;
do
{
Console.WriteLine(x);
x++;
} while(x <=10);
Hints & tips
- While loops continue looping WHILE a condition is true.
- You can use a regular While loop or a Do While loop as needed.
- 00:04 Okay in this video we want to look at the while loop and the do while loop.
- 00:07 And loops are super important in any programming language.
- 00:10 They're a fundamental concept that you're going to use over and over again.
- 00:13 And a loop does just what it sounds like, it loops, it goes through things over and
- 00:17 over and over again until you tell it to stop, until some condition is met.
- 00:21 So there are a few basic loop types, and
- 00:22 in this video we're going to look at the while loop.
- 00:25 So to use almost any loop, you're almost always going to need a counter,
- 00:28 some way to keep track of how many times you've been looping through the loop.
- 00:32 So let's create a counter.
- 00:33 This is going to be an integer, and let's just call it x.
- 00:35 You could call it anything you want.
- 00:36 You could call it counter.
- 00:38 You could call it count.
- 00:39 You could call it x.
- 00:40 And let's just set this equal to 1 to start out with.
- 00:43 So, to create a while loop, we just type in while, and
- 00:45 then we define the condition.
- 00:47 So let's say while x is, let's say less than or equal to 10,
- 00:52 we want to do something.
- 00:54 So, remember our comparison operators, we're saying hey, right now it's 1.
- 00:58 Keep looping, as long as x is less than or equal to 10,
- 01:02 right, so inside of here, we can create our loop.
- 01:06 And we could do anything we want.
- 01:08 So let's for instance, console write out something.
- 01:13 And let's just write out x to see what's going on here.
- 01:16 So if we run this, we're going to get an infinite loop.
- 01:18 It's just going to keep looping forever.
- 01:20 Because x equals 1, it'll print out 1, loop again, x will be less than or
- 01:24 equal to 10, still.
- 01:25 Because it's 1, it will print out 1 again, loop through it again, and
- 01:30 it'll just keep doing that forever.
- 01:33 Because we're not changing the counter.
- 01:35 We need to actually increment the counter.
- 01:36 Remember we talked about our increment operator way back at the beginning of
- 01:39 the course.
- 01:40 We can just go x ++.
- 01:42 All right, so now, it will print it out to the screen, add 1 to it,
- 01:46 and then loop again.
- 01:48 Well now, x will be 2.
- 01:49 So 2, is still less than or equal to 10.
- 01:53 So it'll print out 2, add another 1, 2 becomes 3, loop through, do it again,
- 01:57 4, 5, 6, 7, 8, 9,10.
- 01:59 So it'll stop.
- 02:00 So go ahead and save this.
- 02:04 Run this, see what it looks like.
- 02:08 We can see 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.
- 02:10 And then it stops.
- 02:12 It is now false, the whole thing stops, all right?
- 02:14 That's your basic while loop.
- 02:16 Here we added, we could also subtract.
- 02:18 So let's start out with say, 10.
- 02:21 Now we could say, as long as x is greater than, or
- 02:24 equal to, let's say 1, we could write it out.
- 02:27 But instead of adding here, now we want to- -, we can decrement, decrease, right?
- 02:35 So we'll start out with 10.
- 02:36 Is 10 greater than or equal to 1?
- 02:38 Yes it is.
- 02:38 It'll print out 10, subtract 1, and loop through it again.
- 02:41 So if we save this and run it, sort of the opposite will happen.
- 02:46 We start out at 10.
- 02:47 Go down, 9, 8, 7, 6, 5, 4, 3, 2, 1, boom.
- 02:49 And that's that.
- 02:49 So that's the while loop, super useful.
- 02:52 There's also a do while loop.
- 02:55 And it's very similar.
- 02:56 So let's say, let's change this back to 1.
- 03:00 And for this guy, let me actually copy this.
- 03:04 The syntax is a little bit different.
- 03:06 Here we just say do, right, and then we have our brackets.
- 03:11 And now inside of here, we do something, all right?
- 03:13 So for instance, let's write out x.
- 03:15 And now you can do as much stuff as you want to do between these brackets.
- 03:19 Same thing with the while loop.
- 03:20 I'm just doing one line.
- 03:21 We're just writing this out.
- 03:23 But you could have 10 pages of code here,
- 03:25 whatever you want to do inside this loop you can, so.
- 03:30 Let's also now increment our counter, x + +.
- 03:34 Now, outside of this, we put our while condition, which is,
- 03:38 you know, a little different.
- 03:40 So we will say, while x is, let's say less than or equal to 10.
- 03:43 So if we save this, now we need a semicolon right there.
- 03:47 Which we didn't earlier in the while loop.
- 03:50 But now, since this is at the end of this whole thing, we do, so, go ahead and
- 03:53 save this and run it.
- 03:56 And as we would expect, does the same thing,1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
- 04:00 And then it ends, so, to be honest, I rarely use a do while loop,
- 04:05 and I hardly ever come across them in other people's code either.
- 04:09 You're usually going to want to stick with your while loops, but
- 04:12 this is available if you need it for any specific reason.
- 04:16 It's just sort of a different syntax basically, in your regular while loop, but
- 04:20 like I said, for the most part, people are going to use just the regular while loop,
- 04:24 it's just sort of more standard than this do loop, but
- 04:27 you have both of them to choose from.
- 04:29 And it's always a matter of style and preference when it comes to coding.
- 04:32 So you'll use the one you like best.
- 04:35 I would recommend using the regular while loop, just because it's more common.
- 04:38 And as you're working with other people,
- 04:40 they're going to be more comfortable with it, as they use the while loop too.
- 04:44 But you have options.
- 04:45 So that's the while loop.
- 04:46 Pretty simple, in the next video, we'll look at the for loop.
Lesson notes are only available for subscribers.