Pythonbreak, continue & pass

break, continue & pass

Python gives you three small statements that change how a loop (or a block of code) behaves: break stops a loop dead in its tracks, continue skips ahead to the next iteration, and pass does absolutely nothing — it exists purely so that Python has a valid statement to put where one is syntactically required. They look simple, but using them correctly makes loops (and stub code) much easier to read than piling up nested if conditions.

break — exit the loop entirely

break immediately terminates the nearest enclosing for or while loop. Execution jumps to the first statement after the loop, and any remaining iterations (or loop condition checks) are skipped completely. This is the classic tool for "search and stop as soon as you find it" logic — there's no point scanning the rest of a list once you already have your answer.

Stop scanning once a target is found

Python
numbers = [4, 8, 15, 16, 23, 42]
target = 16

for n in numbers:
    print(f"checking {n}...")
    if n == target:
        print("found it!")
        break
else:
    print("not found")

# Output:
# checking 4...
# checking 8...
# checking 15...
# checking 16...
# found it!
# (23 and 42 are never even looked at)

break works the same way inside a while loop — it's especially handy for "loop until some condition happens inside the loop body" patterns that don't fit neatly into the loop's own condition.

break inside a while loop

Python
import random

random.seed(0)
attempts = 0

while True:
    attempts += 1
    roll = random.randint(1, 6)
    print(f"attempt {attempts}: rolled a {roll}")
    if roll == 6:
        print("rolled a six, stopping")
        break

print(f"took {attempts} attempts")
break only exits the nearest loop
If loops are nested, `break` only escapes the innermost one. To break out of multiple levels, use a flag variable, refactor the inner loop into a function and `return` from it, or restructure with a `for ... else` pattern.
continue — skip to the next iteration

continue skips the rest of the current iteration's body and jumps straight to the next one — for a for loop that means moving on to the next item; for a while loop it means re-checking the loop condition. Unlike break, the loop keeps running; you're just short-circuiting one pass through it.

Skip even numbers in a for loop

Python
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

odd_total = 0
for n in numbers:
    if n % 2 == 0:
        continue
    odd_total += n

print(odd_total)  # 25 (1 + 3 + 5 + 7 + 9)

continue is also useful for filtering out invalid data early, before the "real" body of the loop runs, so you don't have to nest the whole rest of the loop inside an if.

Skip invalid entries while validating input

Python
raw_ages = ["25", "thirty", "17", "-4", "42"]

valid_ages = []
for entry in raw_ages:
    if not entry.isdigit():
        continue
    age = int(entry)
    if age < 0 or age > 120:
        continue
    valid_ages.append(age)

print(valid_ages)  # [25, 17, 42]

And here it is in a while loop, skipping ahead without ending the loop:

continue inside a while loop

Python
n = 0
while n < 10:
    n += 1
    if n % 3 != 0:
        continue
    print(f"{n} is a multiple of 3")

# Output:
# 3 is a multiple of 3
# 6 is a multiple of 3
# 9 is a multiple of 3
pass — the do-nothing placeholder
Python's syntax requires that certain blocks — function bodies, class bodies, if/elif/else branches, except blocks, loop bodies — contain at least one statement. `pass` is a statement that does nothing at all; it's there purely to satisfy the parser. Reach for it whenever you need a placeholder for code you haven't written yet, or when a branch genuinely has nothing to do.

pass as a stub for code you'll fill in later

Python
def calculate_discount(price, customer_tier):
    # TODO: implement tiered discount logic
    pass


class ReportGenerator:
    # TODO: flesh this out once the report format is finalized
    pass

pass in an except block you intentionally want to ignore

Python
import os

try:
    os.remove("cache.tmp")
except FileNotFoundError:
    # It's fine if the cache file was already gone — nothing to do here.
    pass

print("cleanup finished")
Tip
Don't confuse `pass` with `continue` or with `None`. `pass` is a no-op statement — it doesn't skip iterations and it isn't a value. If you find yourself writing `pass` in real (non-stub) code, it usually means "this branch is intentionally empty," which is worth a short comment explaining why.
Quick comparison

Statement

Effect

Typical Use Case

break

Exits the enclosing loop immediately, skipping any remaining iterations

Stop searching once a match is found; exit an infinite while True loop

continue

Skips the rest of the current iteration and moves to the next one

Filter out invalid items or skip cases that need no processing

pass

Does nothing — a syntactic placeholder

Stub functions/classes, empty except blocks, "no-op" branches