break, continue, and pass are three small Python statements that are easy to mix up:
| Statement | What it does |
|---|---|
break | Leaves the loop immediately |
continue | Skips the rest of the current pass and starts the next one |
pass | Does nothing; it fills a block that would otherwise be empty |
break and continue only work inside a for or while loop. pass can go anywhere a statement is required.
break: leave the loop now
break ends the loop at once. Python continues with the first line after the loop.
numbers = [4, 9, 15, 22, 7]
for number in numbers:
if number > 10:
print("First number above 10:", number)
breakThis prints First number above 10: 15. The loop never looks at 22 or 7, because there is no reason to keep searching once the answer is found.
break is also the normal way out of a while True loop:
total = 0
while True:
entry = input("Enter a number (blank to finish): ")
if entry == "":
break
total += float(entry)
print("Total:", total)continue: skip to the next pass
continue abandons the current pass. In a for loop Python moves to the next item. In a while loop it goes back to check the condition.
for number in range(1, 8):
if number % 2 == 0:
continue
print(number)This prints 1, 3, 5, and 7. Each even number triggers continue, so the print line is skipped for it.
continue is useful for ignoring items you do not want to process:
lines = ["alice@example.com", "", "# comment", "bob@example.com"]
for line in lines:
if line == "" or line.startswith("#"):
continue
print("Sending to", line)In a while loop, make sure the update runs before continue. If continue skips the line that changes the loop variable, the condition never changes and the loop runs forever.
n = 0
while n < 5:
n += 1 # update first
if n == 3:
continue
print(n) # prints 1, 2, 4, 5pass: do nothing
pass is a statement that has no effect. Python does not allow an empty block, so pass stands in when a block is required but you have nothing to put there yet.
def send_invoice(order):
pass # to be written later
for item in ["a", "b", "c"]:
pass # an empty loop is valid with passWithout pass, both blocks would be an IndentationError.
pass is also used to deliberately ignore a case:
value = "42a"
try:
number = int(value)
except ValueError:
pass # not a number; leave it unconvertedpass is not continue
This is the difference people most often get wrong. continue jumps to the next pass. pass does nothing, so the code below it still runs.
for n in range(1, 4):
if n == 2:
pass
print(n) # prints 1, 2, 3
for n in range(1, 4):
if n == 2:
continue
print(n) # prints 1, 3break and continue in nested loops
In a loop inside another loop, break and continue affect only the innermost loop that contains them:
for row in range(1, 4):
for col in range(1, 4):
if col == 2:
break
print(row, col)This prints 1 1, 2 1, and 3 1. The break ends the inner loop each time, but the outer loop carries on with the next row. Ways to leave both loops at once are covered in nested loops in Python.
break and the loop else clause
A loop's else block runs only if the loop was not ended by break. That makes a neat "not found" branch:
users = ["ada", "grace", "linus"]
for user in users:
if user == "guido":
print("Found guido.")
break
else:
print("guido is not in the list.")When to use each one
- Use
breakwhen you have found what you were looking for or cannot continue. - Use
continueto skip an item early and keep the main logic of the loop unindented. - Use
passas a placeholder while you are still writing the code, or to say clearly that nothing should happen.
Use break and continue sparingly. A loop with several of each is hard to follow, and a clearer condition is often the better fix. For the loops themselves, see Python for loops explained.
Rune AI
Key Insights
- break leaves the loop immediately; code after the loop runs next.
- continue skips the rest of the current pass and moves to the next one.
- pass does nothing and exists to fill a block that cannot be left empty.
- pass is not continue: the lines after pass still run.
- In nested loops, break and continue affect only the innermost loop.
- A loop's else block runs only when the loop was not ended by break.
Frequently Asked Questions
What is the difference between break and continue in Python?
What is the difference between pass and continue in Python?
What does pass do in Python?
Does break exit all loops in Python?
Can I use break outside a loop?
Conclusion
break stops a loop, continue skips one pass, and pass is an empty placeholder. Reach for break when the work is finished early, continue when an item should be ignored, and pass when a block has nothing to do yet.
More in this topic
Nested Loops in Python
A nested loop is a loop inside another loop. See exactly what order the lines run in, how to work with grids and nested lists, and how to break out of both loops.
Python while Loops Explained
A while loop repeats a block for as long as a condition stays true. Learn the syntax, the while True pattern, how to validate input, and how to avoid infinite loops.
Common Python Mistakes Beginners Make
Twelve mistakes nearly every Python beginner makes, with the exact error message each one produces and the fix. Learn to recognise them and you will debug much faster.