Three small keywords change how loops behave. They look similar but do very different things.
break — exit the loop early
break jumps out of the current loop immediately, skipping the rest of the iteration and any remaining iterations:
for n in range(1, 11):
if n == 5:
break
print(n)
1
2
3
4
When n becomes 5, the break runs and the loop ends. Numbers 5 through 10 are never printed.
break is most useful for searching:
numbers: list[int] = [3, 7, 12, 4, 18, 9]
target: int = 12
found_at: int = -1
for index, value in enumerate(numbers):
if value == target:
found_at = index
break
if found_at >= 0:
print(f"Found {target} at index {found_at}")
else:
print(f"{target} not in list")
We stop as soon as we find the target — no need to keep checking the rest.
continue — skip to the next iteration
continue skips the rest of the current loop body and moves on to the next iteration:
for n in range(1, 11):
if n % 2 == 0:
continue
print(n)
1
3
5
7
9
For each even n, the continue runs and print(n) is skipped.
continue is useful when you want to filter items inside a loop:
scores: list[int] = [45, 78, 32, 91, 50, 25]
passing: list[int] = []
for score in scores:
if score < 50:
continue
passing.append(score)
print(passing) # [78, 91, 50]
Often a list comprehension (Section 6) is even cleaner — but continue keeps explicit loops readable.
break vs continue
break — leave the loop entirely
continue — skip the rest of this iteration, do the next one
A picture helps:
for i in [1, 2, 3, 4, 5]:
if i == 3:
break # → loop ends, prints 1, 2
print(i)
for i in [1, 2, 3, 4, 5]:
if i == 3:
continue # → skips 3, prints 1, 2, 4, 5
print(i)
pass — do nothing
We met pass in the if/elif/else lesson. It’s a placeholder that does literally nothing. Useful when Python’s syntax requires a block but you haven’t written one yet:
for n in range(5):
if n == 2:
pass # TODO: handle this case
else:
print(n)
It’s also the standard “empty function” body:
def not_implemented_yet() -> None:
pass
Don’t confuse pass with continue:
passdoes nothing and lets the next line in the block run normally.continuestops the current iteration and jumps to the next.
for/else and while/else (advanced)
Python has a feature that surprises most people coming from other languages — loops can have an else clause. The else runs only if the loop finishes without hitting break:
numbers: list[int] = [1, 2, 3, 4]
target: int = 7
for n in numbers:
if n == target:
print(f"Found {target}")
break
else:
print(f"{target} not in list")
If the target was in the list, break runs and else is skipped. If the loop completes without break, else runs.
It’s a useful pattern, but most Python developers forget it exists. Don’t worry about memorising it — just recognise it if you see it in real code.
What’s next
You can break out of loops, skip iterations, and write empty placeholder blocks. Next, a closer look at range() and what it means for something to be iterable.