Python’s break and continue both change how a loop runs. break exits the loop immediately, and continue skips the rest of the current pass and moves on to the next one. Both work in for and while loops.
for amount in orders:
if amount < 0:
continue # skip this one
if amount > 500:
break # stop the whole loop
The orders, statuses and seat numbers below are made up, but every output is real: I ran each file on Python 3.12.5 in the Command Prompt.
After the basics, you’ll see the for...else pattern, how to get out of nested loops, and the one continue mistake that hangs a while loop.
How to break out of a for loop in Python
Use break when you’ve found what you’re looking for and there’s no point checking the rest. Here the loop stops at the first order over $500:
orders = [129.99, 48.50, 612.00, 89.95, 1250.00]
for amount in orders:
print("checking", amount)
if amount > 500:
print("needs approval:", amount)
break # stop the loop right here
print("done")
Output:
checking 129.99
checking 48.5
checking 612.0
needs approval: 612.0
done
done still printed, because code after the loop runs as normal.break only ends the loop. It doesn’t end the function or the program, so everything after the loop still runs.
If you want to leave the whole function, use return instead; exiting a function in Python covers that.
How to skip an iteration with continue
continue is for the items you want to ignore. Python jumps straight to the next item, skipping whatever is below it in the loop body:
orders = [
("A101", "paid", 129.99),
("A102", "cancelled", 48.50),
("A103", "paid", 612.00),
("A104", "refunded", 89.95),
]
total = 0
for order_id, status, amount in orders:
if status != "paid":
print("skipping", order_id, status)
continue # jump to the next order
total += amount
print(f"paid total: ${total:,.2f}")
Output:
skipping A102 cancelled
skipping A104 refunded
paid total: $741.99
You could write the same thing with an if around the code you want to run. continue keeps the main logic unindented, which reads better when the skip conditions pile up.
Using break to exit a while True loop
while True loops forever on purpose, and break is how you get out. It’s the natural shape for retries, menus and anything that runs until a condition is met:
responses = ["timeout", "timeout", "ok"] # what the server answers each time
attempt = 0
while True:
status = responses[attempt]
attempt += 1
print(f"attempt {attempt}: {status}")
if status == "ok":
break # the only way out of while True
if attempt == 5:
print("giving up")
break
print("finished after", attempt, "attempts")
Output:
attempt 1: timeout
attempt 2: timeout
attempt 3: ok
finished after 3 attempts
I always give a while True loop a second way out, like the five-attempt limit here. If the good case never comes, it stops instead of running forever.
For the condition-based style of while loop, see Python while loop conditions.
Why does continue cause an infinite while loop?
In a for loop, the next item arrives automatically. In a while loop, you move the counter yourself, so a continue placed before that line skips it forever:
# continue jumps back to the condition, skipping everything below it
n = 0
steps = 0
while n < 5:
steps += 1
if steps > 12:
print("stuck: n never got past", n)
break # safety net so the demo can't hang
if n == 2:
continue # n += 1 below never runs again
n += 1
# fix: update the counter BEFORE continue
n = 0
while n < 5:
n += 1
if n == 3:
continue
print("processing", n)
Output:
stuck: n never got past 2
processing 1
processing 2
processing 4
processing 5
The first loop got stuck at 2 and only stopped because of the safety check. The fix is to update the counter before any continue, as the second loop does.
Python while loop continue goes deeper on this, with more patterns for skipping inside a while loop.
Using for…else with break
A for loop can have an else block. It runs only when the loop finishes without hitting break, which makes it perfect for “search, and say so if nothing matched”:
inventory = {"SKU-1001": 0, "SKU-1002": 14, "SKU-1003": 3}
def find(sku):
for item in inventory:
if item == sku:
print(sku, "found, stock:", inventory[item])
break
else:
print(sku, "not found") # runs only if the loop never hit break
find("SKU-1002")
find("SKU-9999")
Output:
SKU-1002 found, stock: 14
SKU-9999 not found
break, so else was skipped. The second never did, so it ran.The name trips people up; think of it as “if no break”. It works on while loops too.
How do you break out of nested loops in Python?
break only leaves the innermost loop. To stop both loops at once, put them in a function and use return:
seats = [["A1", "A2"], ["B1", "B2"], ["C1", "C2"]]
taken = {"A1", "A2", "B1"}
print("-- break leaves only the inner loop --")
for row in seats:
for seat in row:
if seat not in taken:
print("first free:", seat)
break # the outer loop keeps going
print("-- return leaves both loops --")
def first_free(seats, taken):
for row in seats:
for seat in row:
if seat not in taken:
return seat
return None
print("first free:", first_free(seats, taken))
Output:
-- break leaves only the inner loop --
first free: B2
first free: C1
-- return leaves both loops --
first free: B2
break, every row reported its first free seat. With return, the search stopped at B2.A flag variable that you check after the inner loop also works, but a small function is usually cleaner and gives the search a name.
break vs continue vs pass in Python
They look similar but do very different things. pass is a placeholder that does nothing at all, so the code after it still runs:
for n in [1, 2, 3]:
if n == 2:
pass # does nothing; the print still runs
print("pass:", n)
for n in [1, 2, 3]:
if n == 2:
continue # skips the rest of this iteration
print("continue:", n)
Output:
pass: 1
pass: 2
pass: 3
continue: 1
continue: 3
| Statement | What it does | Typical use |
|---|---|---|
break | Leaves the loop immediately | Stop when you find a match or hit a limit |
continue | Skips to the next iteration | Ignore items that don’t qualify |
pass | Does nothing | An empty block you’ll fill in later |
return | Leaves the whole function | Stop nested loops at once |
SyntaxError: ‘break’ outside loop
break and continue only make sense inside a loop. Use one anywhere else, even inside an if in a function, and Python refuses to run the file at all:
def check(amount):
if amount > 500:
break # not inside a loop
check(600)
Traceback:
File "C:\pyguides\break_outside.py", line 3
break # not inside a loop
^^^^^
SyntaxError: 'break' outside loop
To stop a function early, use return. To stop the whole program, use sys.exit(); Python exit commands compares the options.
If you’re working with loops, these guides are worth reading too:
- Python while loop continue
- For loop vs while loop in Python
- Python for loop with index
- for i in range() in Python
- Python list comprehension
Frequently asked questions
What is the difference between break and continue in Python?
break ends the loop completely. continue ends only the current iteration and moves on to the next one.
How do I exit a for loop early in Python?
Use break inside the loop, usually behind an if. Code after the loop still runs.
How do I break out of two nested loops?
Put the loops in a function and return from the inner loop. break on its own only leaves the innermost loop.
When does the else block of a for loop run?
Only when the loop finishes without hitting break. If the loop breaks, the else block is skipped.
Why is my while loop with continue running forever?
The continue skips the line that updates your counter. Move the update above the continue.
Can I use break inside an if statement?
Yes, as long as that if is inside a loop. Outside a loop, it’s a SyntaxError. The Python tutorial covers break, continue and loop else clauses.
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn