Python From Scratch #28 - break and continue
Python From Scratch #28 - break and continue
Learn how to stop a loop early or skip an iteration using break and continue.
Hey 👋
In the last lesson, we learned about while loops and how they repeat code while a condition is True.
But sometimes, you don’t want a loop to continue normally.
You may want to:
stop the loop completely
skip the current iteration and continue with the next one
That’s where break and continue come in.
The break Statement
The break statement stops a loop immediately.
For example:
for number in range(1, 11):
if number == 5:
break
print(number)
# Output:
# 1
# 2
# 3
# 4When number becomes 5, the if condition becomes True.
The break statement then stops the loop.
The numbers after 5 are never printed.
How It Works
The loop starts normally:
1 → print
2 → print
3 → print
4 → print
5 → breakOnce break runs, Python exits the loop completely.
break With a while Loop
You can also use break inside a while loop.
number = 1
while number <= 10:
if number == 5:
break
print(number)
number += 1
# Output:
# 1
# 2
# 3
# 4Even though the loop condition is number <= 10 the loop stops when number reaches 5 because of break.
A Common while True Pattern
Yesterday, we saw that an infinite loop can happen when the condition of a while loop never becomes False.
But an infinite loop isn’t always a mistake.
A very common pattern is to intentionally use while True and then use break when you want to stop the loop.
For example:
while True:
answer = input("Type 'exit' to quit: ")
if answer == "exit":
break
print("You typed:", answer)Here, while True means the loop will keep running.
The loop only stops when the user types "exit".
When that happens, this condition becomes True:
if answer == "exit":Then break stops the loop.
Yesterday, we treated infinite loops as something to avoid. But while True combined with break is actually a common, intentional pattern. It keeps running until a specific condition inside the loop tells it to stop.
You’ll see this pattern often when building programs that need to keep asking for input until the user chooses to quit.
The continue Statement
The continue statement works differently.
Instead of stopping the whole loop, it skips the current iteration and moves to the next one.
For example:
for number in range(1, 6):
if number == 3:
continue
print(number)
# Output:
# 1
# 2
# 4
# 5When number is 3, continue skips the rest of that iteration.
The loop then continues with 4.
How It Works
1 → print
2 → print
3 → skip
4 → print
5 → printThe loop itself doesn’t stop.
Only that particular iteration is skipped.
break vs continue
The easiest way to remember the difference:
Statement What it does
break Stops the entire loop
continue Skips the current iterationWhat About Nested Loops?
A loop can also be placed inside another loop. This is called a nested loop.
When you use break or continue inside nested loops, they only affect the innermost loop they’re written in.
For example, if a for loop is inside another for loop, a break inside the inner loop stops the inner loop, not the outer one.
Example: Skip Even Numbers
We can use continue to skip numbers we don’t want.
for number in range(1, 11):
if number % 2 == 0:
continue
print(number)
# Output:
# 1
# 3
# 5
# 7
# 9Here, % gives us the remainder.
If the remainder is 0, the number is even, so continue skips it.
Example: Stop When You Find a Number
You can use break when you want to stop searching after finding something.
for number in range(1, 11):
if number == 7:
print("Found it!")
break
print(number)
# Output:
# 1
# 2
# 3
# 4
# 5
# 6
# Found it!Once 7 is found, the loop stops.
Try It Yourself
Now, a small exercise for you.
Try these:
Write a
forloop from1to10. Usebreakto stop the loop when the number reaches5.Write a
forloop from1to10. Usecontinueto skip the number5.Use
continueto print only the odd numbers from1to10.
That’s all for today!
Tomorrow, we’ll learn about else with loops and see how it works differently from the else you’ve already used with if statements.
If you enjoyed this newsletter, consider sharing it with another developer. And if you have any feedback, I’d love to hear it. It would genuinely mean a lot to me. ❤️
Want to sponsor my work? Let’s talk!
See you tomorrow ✨
- Shefali
Thanks for reading DevBits by Shefali! Subscribe for free to receive new posts and support my work.
If you enjoyed this edition of DevBits, you can support my work here ☕
Your support helps me keep creating more developer tips, UI snippets, and helpful content for the community.
© 2026 Shefali Jangid
548 Market Street PMB 72296, San Francisco, CA 94104