Locked lesson.
About this lesson
In this lesson, we explain how to Break and Continue loops.
Exercise files
Download this lesson’s related exercise files.
38 - Loops Break and Continue.docx60.9 KB 38 - Loops Break and Continue SOLUTION.docx
58.9 KB
Quick reference
Loops: Break and Continue
Break allows us to break out of a loop. Continue allows us to break one iteration of the loop and then continue on.
When to use
Use these when you need to break out of a loop and/or continue on afterward.
Instructions
Break:
int x = 8;
while (x >= 1 )
{
if (x == 4)
break;
Console.WriteLine(x);
x--;
}
Continue:
int x = 8;
while (x >= 1 )
{
if (x == 4)
{
x--;
continue;
}
Console.WriteLine(x);
x--;
}
Hints & tips
- Break allows you to break out of a loop
- Continue allows you to break one iteration of the loop and then continue on
- 00:01 Okay, in this video, I want to talk about breaking and
- 00:05 continuing in and out of loops.
- 00:08 So you've got a loop, it's looping around, it's doing its thing.
- 00:11 You may want to break out of that loop before the loop actually in.
- 00:16 So you know in our past videos, we started at 10 and went down to 1,
- 00:19 it ends when we hit 1, right?
- 00:21 Well, maybe we want to end when it hits 5, even though it should keep going.
- 00:26 Or maybe we want to do something else when it hits 5 and
- 00:29 continue on after the normal 5.
- 00:32 So you can do either one by breaking or continuing and
- 00:34 that's what we're going to look at in this video.
- 00:36 So let's create a quick while loop.
- 00:37 So let's create an integer, let's call it x, let's set it = to, I don't know,
- 00:41 8, whatever.
- 00:42 We don't have to start it 1 or 0, or 10 or anything, you start anything you want for
- 00:47 your counter.
- 00:48 And let's go while and say x is greater than or
- 00:52 equal to 1, we want to do something.
- 00:55 And what we want to do, let's cConsole.WriteLine and
- 01:00 let's just print out x, right?
- 01:03 And let's say x- -, so we're starting at 8, we want to go down.
- 01:07 So if we save this and run it, we know what's going to happen.
- 01:14 We started 8, 7, 6, 5, 4, 3, 2, 1.
- 01:15 Well, what if we want to break when we're halfway through, or a certain way through?
- 01:21 Well, we could do that.
- 01:23 We can add a condition inside of here.
- 01:26 So let's go if x = 4, let's just say, then we want to break.
- 01:32 So let's save this and run it, see what happens.
- 01:39 So now we get 8, 7, 6, 5, we hit 4, it breaks, it stops.
- 01:44 It should have kept going 3, 2, 1, but no, no, we told it, hey, stop at 4.
- 01:48 I don't know why you would want to do that but you can and it's just that easy.
- 01:53 We can break out of here.
- 01:55 We can also continue, all right?
- 01:56 What is this going to do?
- 01:58 Well, this is going to sort of skip a step, right?
- 02:02 If x = 4, it's saying start with the next loop again,
- 02:06 don't do anything for 4, keep going after that.
- 02:11 So here we can go if x = 4 and let's create some brackets,
- 02:16 let's increment or decrement our counter and then say continue.
- 02:22 We'll save this and run it.
- 02:26 Now, we get at 8, 7, 6, 5, 4 isn't there, 3,
- 02:31 2, 1, we've essentially skipped 4 and then continued on after it, right?
- 02:38 And that's how you do that.
- 02:40 So breaking continue very easy.
- 02:41 We can do these same exact things with our for loop.
- 02:45 So, we created a quick for loop, we could say for int i =, let's say started at 10.
- 02:50 We want to say i greater than or equal to 1, and then let's i --.
- 02:58 Decrement this guy.
- 03:00 Here, we can Console.WriteLine out our i,
- 03:05 and we've got an extra bracket here.
- 03:10 All right, so if we save this and run it, we know this is just going to go to 10, 9,
- 03:14 8, 7, 6, 5, 4, 3, 2,1.
- 03:16 Again, if we want to break out of here, inside of here,
- 03:23 we could go if i = 4, then you know we want to break.
- 03:29 Save this, run it.
- 03:31 Same thing, 10,9, 8, 7, 6, 5, it hits 4, we break, right?
- 03:34 Same thing with continue.
- 03:38 We can add our scrolly brackets and we can say continue.
- 03:45 Same thing here, save it, run it.
- 03:47 And notice we don't have to add our increment or decrement inside
- 03:51 here since this is a for loop, I get 10, 9, 8, 7, 6, 5, 4 disappears.
- 03:55 We continue after 4, 3, 2, 1.
Lesson notes are only available for subscribers.