Break & Continue
in Python Loops
Master the two most powerful loop control statements that let you exit early or skip iterations on command.
Python
What Are Loop Control Statements?
Normally, a loop runs from start to finish. But sometimes you need to change that behavior mid-loop: maybe you found what you were searching for, or you want to skip invalid data. Python gives you two keywords for this.
break
Immediately exits the entire loop. No more iterations run.
continue
Skips the rest of the current iteration and jumps to the next one.
Why Do We Need Them?
1
Efficiency: Stop searching once you have found the answer instead of checking every remaining item.
2
Data filtering: Skip over invalid or unwanted entries without adding extra nesting.
3
Cleaner code: Reduce deep if/else nesting by handling special cases at the top of the loop body.
4
Safety: Prevent infinite loops from running forever by breaking on a sentinel condition.
How break Works
How continue Works
The Formal Rules
break
1. Terminates the innermost enclosing loop.
2. Execution jumps to the first statement after the loop.
3. In a for loop, the else clause is not executed if break was triggered.
2. Execution jumps to the first statement after the loop.
3. In a for loop, the else clause is not executed if break was triggered.
continue
1. Skips the rest of the current loop body.
2. In a for loop, the loop variable advances to the next item.
3. In a while loop, the condition is re-evaluated immediately.
2. In a for loop, the loop variable advances to the next item.
3. In a while loop, the condition is re-evaluated immediately.
break vs continue: Key Distinction
| break | continue | |
|---|---|---|
| Effect on loop | Exits entirely | Skips to next iteration |
| Remaining iterations | Never run | Still run normally |
| Code after the statement | Skipped (inside loop) | Skipped (this iteration only) |
| for/else clause | Else is skipped | Else still runs |
| Typical use | Search found, error stop | Filter items, skip invalid data |
How to Apply Them
1
Identify your goal: Do you need to stop the loop entirely, or just skip one iteration?
2
Write the condition: Place an if statement that tests whether the special case has occurred.
3
Choose the keyword: Use break to exit or continue to skip. Place it directly inside the if block.
4
Place it early: Put the guard clause near the top of the loop body so skipped code is obvious.
5
Test edge cases: Verify the loop behaves correctly when the condition is always true or never true.
Example 1: break in Search
numbers = [4, 7, 2, 9, 1, 5] for n in numbers: if n == 9: print("Found 9!") break print(f"Checking {n}...")
Output
Checking 4... Checking 7... Checking 2... Found 9!
Example 2: continue to Filter
scores = [85, -1, 92, -1, 78] for s in scores: if s < 0: continue # skip invalid print(f"Valid score: {s}")
Output
Valid score: 85 Valid score: 92 Valid score: 78
Example 3: break in a while Loop
while True: user = input("Enter 'q' to quit: ") if user == "q": print("Goodbye!") break print(f"You typed: {user}")
Example 4: break + continue Together
data = ["alice", "", "bob", "STOP", "carol"] for name in data: if name == "": continue # skip blanks if name == "STOP": break # stop processing print(name.title())
Output
Alice Bob
Nested Loops: Only the Innermost
for i in range(3): for j in range(3): if j == 1: break # exits inner loop only print(f"({i}, {j})")
Output
(0, 0) (1, 0) (2, 0)
The for/else Pattern
primes = [2, 3, 5, 7] for p in primes: if p == 4: print("Found 4!") break else: print("4 is not in the list.")
Output
4 is not in the list.
Common Mistakes
✕ Breaking too early
Placing break before the processing code means the current item is never handled.
✕ Confusing break with continue
Using break when you meant continue stops the entire loop instead of just skipping one item.
✕ Forgetting nested scope
Expecting break inside an inner loop to also exit the outer loop. It only exits the innermost one.
Summary at a Glance
| break | continue | pass | |
|---|---|---|---|
| What it does | Exits the loop | Skips to next iteration | Does nothing (placeholder) |
| Loop continues? | No | Yes | Yes |
| Triggers for/else? | Skips else | Else still runs | Else still runs |
| Common use | Early exit on match | Skip invalid items | Empty block stub |
Question 1 of 5
What does break do when executed inside a loop?
break terminates the innermost loop entirely. Choice A describes continue, not break.
Question 2 of 5
What is the output of this code?
for i in range(5): if i == 3: continue print(i, end=" ")
continue skips only iteration \( i = 3 \). All other values (0, 1, 2, 4) are printed normally.
Question 3 of 5
What is the output of this code?
for i in range(5): if i == 3: break print(i, end=" ")
When \( i = 3 \), the break fires before the print, so \(3\) is never printed. The loop exits immediately, so \(4\) is also never reached.
Question 4 of 5
In a nested loop, which loop does break exit?
break always exits only the innermost enclosing loop. To exit multiple levels, you need additional logic such as flags or a function with return.
Question 5 of 5
What does the else clause of a for loop execute?
for x in items: if condition(x): break else: do_something()
The else clause of a for loop runs when the loop finishes naturally, meaning break was never executed. If break fires, else is skipped entirely.