Break VS Continue VS Pass In Python Loops Explained In 4 Minutes

You’ve probably seen these 3 keywords — break continue and pass — after working with Python for a while (maybe continue to a lesser extent). Here, let’s explore what each of these keywords do in loops, how to use them and when to use them.
The ‘pass’ keyword
Let’s start with the easiest, the pass keyword. The pass keyword literally tells Python to do nothing.
for i in range(5):
pass # literally do nothingWe usually use the pass keyword when we haven’t decided what to write inside a function/loop yet, so we just pass so that a SyntaxError won’t be raised. Note that this does not affect our loops in any way
for i in range(5):
print(i, end=' ') # print i itself
pass # do nothing, which doesn't affect anything else
print(i**2) # print i^2
# 0 0
# 1 1
# 2 4
# 3 9
# 4 16The ‘break’ keyword
When the break keyword runs, we get out of whatever loop we are in.
for i in range(5):
print(i)
if i==2: # break if i equals to 2
break
# 0
# 1
# 2^ notice that 3 and 4 are not printed. This is because of the conditional break statement — if i==2 we break out of the for loop, stopping the for loop completely at that point in time.
fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']
for fruit in fruits:
print('eating', fruit)
if fruit == 'pear':
break
# eating apple
# eating orange
# eating pearHere, the for loop is forcefully stopped if fruit == 'pear'. Which means that it doesn’t move on to pineapple or banana.
Order matter for the ‘break’ keyword
fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']
for fruit in fruits:
print('eating', fruit)
if fruit == 'pear':
break
# eating apple
# eating orange
# eating pearfruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']
for fruit in fruits:
if fruit == 'pear':
break
print('eating', fruit)
# eating apple
# eating orangeIn these 2 examples, we switched the print('eating', fruit) and if fruit=='pear': break statements. This matters quite a lot in determining what our program does.
In the top example, the print statement happens before the break statement. Therefore pear prints before the for loop breaks.
In the bottom example, the print statement happens after the break statement. Thus, pear does not print.
The ‘continue’ keyword
The continue keyword allows us to skip certain iterations in a loop. In other words, when the continue keyword runs, the current iteration is stopped, and we move on the the next iteration.
fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']
for fruit in fruits:
if fruit == 'pear':
continue
print('eating', fruit)
# apple
# orange
# pineapple
# banana^ here, we continue if fruit equals to ‘pear’. Which means we skip the iteration where fruit equals to ‘pear’. Note that unlike break, the iterations that happen after ‘pear’ still happen.
Order matters for the ‘continue’ keyword
Lines inside the loop that happen before the continue keyword will still be run. The continue keyword skips only the lines that happen after it.
fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']
for fruit in fruits:
if fruit == 'pear':
continue
print('eating', fruit)
# apple
# orange
# pineapple
# bananafruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']
for fruit in fruits:
print('eating', fruit)
if fruit == 'pear':
continue
# apple
# orange
# pear
# pineapple
# bananaIn the top example, the print('eating', fruit) line happens after the continue statement. We are thus able to skip the print line when fruit equals to 'pear'
In the bottom example, the print('eating', fruit) line happens before the continue statement. Which means that this print line will happen for every single iteration. The continue statement for the iteration where fruit == 'pear' still runs, but it doesn’t skip anything because there is nothing under that line.
‘break’ and ‘continue’ in nested loops
Note that both break and continue apply only to the for/while loop that it is directly inside of.
for i in 'abc':
for n in '123':
if n == '2':
break
print(i, n)
# outer loop a
# inner loop 1
# outer loop b
# inner loop 1
# outer loop c
# inner loop 1Here, we have 2 nested for loops — the break statement being in the inner for loop (breaking if n == '2'). Note that the break does not affect the outer for loop for i in 'abc' at all.
The continue keyword behaves the same way — it affects only the loop that it is directly in.
Conclusion
Hope this was clear and helpful
Some Final words
If this story provided value and you wish to show a little support, you could:
- Clap 50 times for this story (this really, really helps me out)
- Sign up for a Medium membership using my link ($5/month to read unlimited Medium stories)
My Home Office Setup: https://zlliu.co/workspace
My Free Ebooks: https://zlliu.co/books
Level Up Coding
Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the Level Up Coding publication
- 💰 Free coding interview course ⇒ View Course
- 🔔 Follow us: Twitter | LinkedIn | Newsletter
🚀👉 Join the Level Up talent collective and find an amazing job
