Python break vs continue vs pass: Differences
break stops the whole loop, continue skips the rest of the current iteration and moves to the next one, and pass does nothing at all and is only a placeholder where Python needs a statement.These three keywords look similar because each one appears inside code blocks and changes how a program flows, but they solve different problems. break and continue only make sense inside a loop. pass works anywhere a statement is required, such as an empty function or class.
You will meet break and continue in the for loop and while loop lessons, and the break and continue lesson goes deeper. pass is covered in Python pass.
for n in range(1, 6):
if n == 3:
break
print(n)break vs continue vs pass comparison table
| Aspect | `break` | `continue` | `pass` |
|---|---|---|---|
| What it does | Ends the loop immediately | Skips to the next iteration | Does nothing |
| Allowed where | Inside for or while only | Inside for or while only | Anywhere a statement is needed |
| Code after it in the loop body | Not run | Not run for this iteration | Still runs |
Triggers the loop's else? | No, it skips the else | No effect on else | No effect |
| Nested loops | Exits only the innermost loop | Affects only the innermost loop | Not relevant |
| Typical use | Stop once you have found what you need | Ignore invalid or unwanted items | Stub for code you will write later |
When to use break
Use break when continuing the loop is pointless.
- Searching: stop as soon as you find the matching item.
- Menus and games: leave a
while True:loop when the user chooses to quit.
Because break skips the loop's optional else block, the pair works well for searches. See the loop else lesson.
When to use continue
Use continue to skip one unwanted item but keep looping.
- Ignoring blank lines, negative numbers or invalid records.
- Avoiding deep nesting: an early
continueoften replaces a largeifblock.
In a while loop, make sure the loop variable is updated before continue runs, otherwise you can create an infinite loop.
When to use pass
Use pass when the syntax demands a body but you have nothing to put there yet.
- Empty function or class while sketching a program:
def save(): pass. - An
exceptblock that deliberately ignores an error (use sparingly and be specific about the exception).
pass is not a loop-control statement. It does not skip anything, so in a loop the lines after it still run. For a comment-only placeholder, ... (Ellipsis) is another common choice.
Examples you can run
Press Run, then edit the code and run it again.
break stops the loop
The search ends when the first negative balance is found, so later items are never checked.
balances = [120, 45, -10, 300]
for amount in balances:
if amount < 0:
print("overdrawn:", amount)
break
print("ok:", amount)Expected outputok: 120
ok: 45
overdrawn: -10
continue skips one item
The loop keeps going after skipping the negative value.
balances = [120, 45, -10, 300]
total = 0
for amount in balances:
if amount < 0:
continue
total += amount
print(total)Expected output465
pass does nothing
The print after pass still runs for every item, which shows that pass does not change the flow.
for n in range(3):
if n == 1:
pass
print("n =", n)Expected outputn = 0
n = 1
n = 2
Common mistakes
Using break outside a loop
✗ Wrong
x = 5
if x > 3:
break✓ Right
for x in range(10):
if x > 3:
break
print(x)break and continue are valid only inside for and while. To leave a function use return.
Expecting pass to skip an iteration
✗ Wrong
for n in range(4):
if n % 2 == 0:
pass
print(n)✓ Right
for n in range(4):
if n % 2 == 0:
continue
print(n)Use continue to skip the rest of the iteration.
Python break vs continue vs pass: Differences: questions people ask
What is the difference between break and continue in Python?
break exits the loop completely, while continue only skips the current iteration and carries on with the next one.
What does pass do in Python?
pass is a null statement that does nothing. It exists so you can write an empty block, such as a function you plan to implement later.
Does break exit all nested loops?
No. It exits only the innermost loop that contains it. To leave several loops, use a function with return or a flag variable.
Keep learning
Step-by-step lessons:
Python break and continuePython pass StatementPython for Loops
Related:
Python else on LoopsPython while LoopsPython Nested Loops
More in operators and syntax:
Python is vs == Operator: What Is the Difference?Python Division (/) vs Floor Division (//) See all →