When a loop has reached the point where it should stop, you need a way to end it before it checks more items or repeats again. Python’s break statement ends the for or while loop that contains it.
I’ll show how to use break in for and while loops, then explain where execution continues after the loop ends.
TL;DR: Python break statement
break ends the nearest enclosing for or while loop as soon as Python reaches it. Execution then continues with the first statement after that loop.
- Put break inside a loop, usually under an if condition.
- In nested loops, it ends only the inner loop that contains it.
- A loop else clause is skipped when break ends the loop.
What is the Python break statement?
Python uses break to leave the nearest enclosing for or while loop, with an if condition deciding when it runs rather than what it exits.
Put the condition in an if statement and indent break inside the loop, since the keyword has no label or condition of its own.
Python break statement syntax and loop setup
A loop provides the context that break needs: for checks items in a sequence, while repeats as long as its condition stays true.
| Loop | Use it when |
|---|---|
| for | Checking items from an iterable. |
| while | Repeating while a condition is true. |
| Either | Indent break inside the loop and its conditional branch. |
How to stop a Python loop with break
Each example places break at the point where further loop iterations are no longer needed. The printed lines show both the stopping point and where execution resumes.
Step 1: Stop a for loop when a match appears
Check each item and break when it matches. I ran this exact example in Python 3.11.16. The match is printed, then the statement after the loop runs.
items = ["oak", "pine", "maple"]
for item in items:
if item == "pine":
print("Found", item)
break
print("Checked", item)
print("After loop")
Checked oak
Found pine
After loop
The loop does not print “Checked maple” because break stops the iteration after finding pine. Execution then reaches “After loop,” which sits outside the loop body.
Step 2: Stop a while loop when a condition changes
A while loop can use break when a condition becomes true during its body, and the counter below shows the value at the stopping point.
remaining = 3
while remaining > 0:
print(remaining)
remaining -= 1
if remaining == 1:
break
print("Stopped at", remaining)
The loop ends at 1, then the final print reports that value from outside the loop.
Step 3: Understand break in nested loops
In nested loops, break exits the nearest enclosing loop, not every loop. This example prints “Next row” after the inner loop ends, including for the row containing the match.
rows = [[1, 2], [3, 4]]
for row in rows:
for value in row:
if value == 3:
print("Match", value)
break
print("Next row")
Next row
Match 3
Next row
The outer loop continues after the inner break. To stop both levels, add an outer-loop condition or move the search into a function and return when the match is found.
Common break edge cases
break changes loop flow only when Python executes it inside a for or while loop. These boundaries answer common questions about if statements and loops that have an else clause.
| Code location | Effect |
|---|---|
| Inside an if nested in a loop | Exits the enclosing loop when the condition is true. |
| Inside a nested loop | Exits only the nearest enclosing loop. |
| Outside every loop | Raises SyntaxError because there is no enclosing loop to exit. |
| Loop else clause | Runs after normal exhaustion, but not after break. |
An if statement does not create a loop, so break cannot be used to leave an if on its own. Use a condition to choose whether to execute break from inside the loop.
continue has a different effect. It skips the rest of the current iteration and starts the next one. Use break to stop the loop, or continue to keep looping while skipping work for one item.
What to do next after using break
After the loop stops, Python runs the next statement outside its body. Check that placement with a small print statement after the loop.
for number in [1, 2, 3]:
if number == 2:
break
print("Loop finished")
Conclusion
Place break in the loop whose repetition should end, and remember that a nested break affects only its nearest loop. For more examples, see the Python for loop guide, the Python while loop guide, and the Python control-flow tutorial.
Can break exit an if statement in Python?
No. break exits the nearest enclosing for or while loop. An if can decide when break runs, but break cannot exit an if that is outside a loop.
Does break stop nested loops in Python?
It stops only the nearest enclosing loop. The outer loop continues unless its own logic also ends it.
Does a loop else clause run after break?
No. A loop else clause runs when the loop finishes without executing break.

