Breaking Out of the Pattern
00:00 Sometimes you get stuck in a loop, and all you need to do is to break out of it. In this lesson, you’ll be covering, bringing loops into the mix. So far, you’ve been looking at conditional statements and creating branches for your program, but how do loops fit into it? And why is that useful?
00:30 Take a look at this example. What this example is trying to do is to take the sum of all even numbers between 0 and 100 and print that sum. So all even numbers between 0 and 100—for example, the first even number between 0 and 100 is 2.
And now we want to check whether this
n is even. The way we can do this is with the modulo operator (
%). The modulo operator divides the number on the left-hand side with the number on the right-hand side and will return the remainder of that division.
So to check if a number is even, you can divide it by
2 and check if there’s no remainder. So you can do
n % 2, and if the remainder is
0, then you know it’s even. So putting that together in an
if statement, you do
if n % 2 == 0: then you can take the
sum_of_evens, set it equal to the
sum_of_evens + n.
Now it’s time to take a look at the
break keyword. Take a look at this quick example,
for n in range(4): So that’s going to be iterating over
4 not included—and it’s doing a check.
if n == 2: then
break and it’s going to print
So on the first iteration, you’ll have
n == 0, so if
n == 2 is
False in that case, so then it’s going to go into
print(n). It’s going to print
1, it’s going to be the same. Then for
2, it’s going to go into this
if statement, and the question is, what does
break do here?
Take a look at this other example of a
while loop, because the
break keyword also works with
while loops. So we’ve got
n starting at
0 and we have a
while True: So if you’re not careful,
while True loops can run forever.
This will print
n, which is
0 on the first iteration, and then if
n > 5, it’s going to break. But if it doesn’t break, then it’s just going to carry on, and it’s going to increment
1 by doing
n = n + 1.
05:00 If your computer seems to hang, then maybe you haven’t broken properly, or the condition you’ve made will never be reached, in which case you can just kill the Python process with your task manager and start again.
So what’s happening here is that it goes through
if block is evaluating to
False, so the indented block corresponding to it doesn’t run, and it just prints
n. But when
n == 2, it will print
"There goes two", and then when it hits the
continue keyword, it will say, okay, this iteration is done—not the whole
for loop, but only this loop of all the other loops to come in the
Here’s another example for you to play around with. Like the previous
while loop example, you’ve got
n = 0 to start off with, and then you’re entering into what could potentially be an infinite
while loop, because the condition is just
In that case, it’s going to break, and then the loop is going to end. And you’ll note that this
print() statement at the end, after the
else block, never gets printed, because this
else block will always run one of its branches, and each of the branches has either a continue or a break statement, which means that no matter what, this
print("end of loop") statement will never get printed. Have a play around with that on the REPL.
In this lesson, you’ve covered bringing loops into the mix, combining loops with conditional statements and branching. You’ve been using the
break keyword to stop a loop dead in its tracks and continue on after the loop, as if the loop had already finished.
Become a Member to join the conversation.