01 / 21

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

LOOP BODY Run code condition? True break False More code... After loop

How continue Works

LOOP BODY Run code condition? True continue next iteration False More code... After loop

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.
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.

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?

Question 2 of 5

What is the output of this code?

for i in range(5):
    if i == 3:
        continue
    print(i, end=" ")

Question 3 of 5

What is the output of this code?

for i in range(5):
    if i == 3:
        break
    print(i, end=" ")

Question 4 of 5

In a nested loop, which loop does break exit?

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()