Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Loop Control Statements .docx59.2 KB Loop Control Statements - Solution.docx
59.1 KB
Quick reference
Loop Control Statements
Loop control statements give you fine tuning control of your loop.
When to use
Use them whenever you need to interrupt a loop.
Instructions
The break statement stops a counter if a condition is met.
count = 0
while (count < 10):
print(count)
count += 1
if (count == 5):
break
The continue statement restarts a loop if a condition is met.
count = 0
while (count < 10):
count += 1
if (count == 5):
continue
print(count)
The pass statement allows us to pause our loop if a condition is met, and then continue right where we left off.
for letter in "John":
if (letter == "h"):
pass
print("This is a pass!")
print(letter)
Hints & tips
- Loop control statements let you interrupt a loop
- Loop control statements are: break, continue, and pass
- 00:04 Okay so we looked at loops already, now I want to talk about loop control
- 00:07 statements, specifically break, continue, and pass.
- 00:10 So let's say you've got a loop and it's looping, it's doing it's thing, but
- 00:14 you need to stop the loop at a certain point based on a certain condition, right.
- 00:19 Well you would use a break, continue or a pass to do that.
- 00:21 You also need an if statement so, if something happens do a break,
- 00:25 if something else happens do a continue, if something else happens do a pass.
- 00:29 So, we're looking at if statements inside of loops basically,
- 00:33 to give you very specific control of your loops.
- 00:35 So, let's create a loop real quick and, I should say, you can use break, continue or
- 00:38 pass in both while loops and for loops, we'll use both of them in this video, so
- 00:42 you'll see them in action for both.
- 00:44 Let's create a variable called counter, set it equal to 0 and
- 00:46 let's create our while loop, while counter is less than 10 and
- 00:51 let's just print out the counter.
- 00:56 And of course, we need to + = increment our counter or else things will go crazy.
- 01:01 Okay, so let's just run this and see.
- 01:03 Well, let's say we want to break our loop and if some condition is met, so
- 01:07 let's go if counter equals 5, break.
- 01:12 And now if we run this, this is 0, 1, 2, 3, 4 when it gets to 5, it breaks.
- 01:18 The whole loop just stops and that's what break does,
- 01:20 it stops if some condition is met.
- 01:23 It allows you to break out of your loop.
- 01:25 That's very useful.
- 01:26 So that's break pretty simple and next we can do continue.
- 01:30 In continue, we need to switch it around a little bit, let's take our counter,
- 01:34 put it at the top and then right after that let's do our if statement.
- 01:38 So if counter equals 5, again continue.
- 01:44 So now if we save this and run it, we see 0, 1, 2, 3, 4,
- 01:48 6, 7, 8, 9, 10, and notice 5 has been skipped.
- 01:53 What happens here is, this control flow starts out at 0,
- 01:57 +- 0 becomes 1 if counter = 5, 1 is not 5.
- 02:01 So it does this, just completely ignores this.
- 02:02 This keeps looping 4, 2, 3, 4 and we get to 5, if counter = 5,
- 02:07 well in this case our counter is 5 so this continue gets triggered.
- 02:11 And what that does is it stops the loop right here.
- 02:14 It does not continue which kind of doesn't make a whole lot of sense because
- 02:17 continued you would think it would keep going.
- 02:19 But continue means, don't continue, stop, and instead go back to the beginning and
- 02:24 start over.
- 02:24 So what happens is, it's 5.
- 02:26 It does not print this, because it stops right here, starts back over at 5.
- 02:31 5 is less than 10, 5 becomes 6, and then we print out 6.
- 02:35 Loops again, 7, 8, 9, 10, all the way to the end.
- 02:38 So we see that we don't have 5 listed.
- 02:40 So that's kind of a little tricky.
- 02:41 Continue doesn't make a whole lot of sense.
- 02:43 It should be don't continue or something but that's continue.
- 02:46 Pass is the last one and let's do a for loop for this one.
- 02:48 Just to mix it up four letter in, let's say John.
- 02:53 And let's just print out our letter.
- 02:56 So if we save this and run it, just a basic for loop, we see J-O-H-N.
- 03:00 Pass allows us to sort of pause and do something and then continue on again.
- 03:05 So with continue, we pause and
- 03:07 we started over with pass, we pause and then continue on.
- 03:11 Let's go right here at the top, let's go, if letter equals,
- 03:17 let's say h, pass, and then tell it what you want to do.
- 03:22 When you use pass, you need to give it some other thing to do.
- 03:25 Otherwise, it'll just pause and then keep going.
- 03:27 So for instance, if we save this right now, it would be the same thing.
- 03:30 J-O-H-N, we haven't told it to do anything so let's tell it to print.
- 03:35 This is a pass.
- 03:37 That's annoying.
- 03:39 So now if we run this we go J-O, and we get to H and it stops, it pauses.
- 03:45 It prints out this line, like we told it to.
- 03:47 And then it just continues on down with the very next thing which is H when
- 03:51 it goes back to N and that's that.
- 03:53 So with continue, it stops and starts over.
- 03:56 With pass, it stops, does a little thing and then just keeps on going.
- 04:00 I'm not sure why they didn't call these opposite things like
- 04:03 pass makes more sense.
- 04:04 If you are going to start over, you're going to pass over, right?
- 04:07 And continue, you're going to continue but for
- 04:09 some reason they named them the opposite.
- 04:11 So break just means completely stop everything if some condition is met.
- 04:15 Continue if the condition is met.
- 04:16 Start over and pass if the condition is met do something and then keep on going.
- 04:21 So those are your loop control statements.
- 04:23 Very, very useful for really fine tuning your loops and
- 04:25 getting exactly what you want out of them.
- 04:28 That's all for loops and loop control.
- 04:29 In the next video we're going to look at FizzBuzz.
Lesson notes are only available for subscribers.