When you work with repetition in Python, some cases force you to leave the loop immediately, without finishing every iteration. Other cases require you to jump straight to the next iteration. But how do you do that?
Python has the break and continue statements to cover both cases. How do you use them? Let's go further.
The BREAK Statement in Python 3
The first one is the break statement. Its job is to force an exit from the loop. In other words, when a break statement appears, the lines after it don't run and the loop is considered finished.
Let's get into an example. Suppose you have code that prints the numbers 1 through 9 with a for loop, like this:
for i in range(1, 10):
print(i)
Try running that code.
The output is a sure thing: the numbers 1 through 9, and nothing strange. Now modify that code a little into this.
for i in range(1, 10):
if i == 5:
break
print(i)
Now run it again. The output will look more or less like this:
1
2
3
4
You can see it. The numbers that were printed 9 times before are now printed only 4 times. The cause is the call to the break statement when the value of i equals 5.
When break is called, the commands below it (in this case print) don't run. That's not all, because the loop is also considered finished.
The break statement isn't only for the for loop, since it works on a while loop too. In real cases, break is very useful when you want to get out of an infinite loop.
Here's an example:
n = 0
while True: // infinite loop
n += 1
print(n)
if n == 5:
break // force exit from the loop
In that example you use the value True as the condition of the while loop, which means the loop will run forever. Because you use the break command when n equals 5, the loop runs only 5 times. So the output becomes like this:
1
2
3
4
5
Now delete the if block along with its break statement, run it, and see what happens.
The CONTINUE Statement in Python 3
Earlier there was break, which exits the loop. Now there is continue, whose job is to jump to the next iteration without executing the rest of the code below it.
The main difference between break and continue is reach. break stops the loop entirely, while continue only jumps to the next iteration. When either one is called, both ignore every command below them.
Let's look at an example, still using the same for loop code as before, but with the break command swapped for continue.
for i in range(1, 10):
if i == 5:
continue
print(i)
Try running it and see what happens. The output will look more or less like this:
1
2
3
4
6
7
8
9
If you look at the output, the number 5 is not printed. Why is that?
The cause is the call to the continue command. So when you enter the condition where i equals 5, the continue command runs. That call makes the iteration jump to the next value of i right away. Because of that, every command below it (in this case print) doesn't run, which is why the number 5 is never printed.
Just like the break statement, continue works on a while loop too. For that example, go ahead and experiment on your own, ehehe...
Closing
That's all for this one, and I hope it's useful. Next time you'll look at the ternary conditional operator in Python 3.
See ya...