Learn Python Programming  /  Control Flow  /  break, continue & pass

break, continue & pass

break, continue, and pass give you fine control over loop execution — and pass gives you a way to write syntactically valid code that does nothing yet.

break — Exit the Loop Immediately

R Code
numbers = [4, 8, 15, 16, 23, 42]

for n in numbers:
    if n > 15:
        print("Found a number over 15:", n)
        break
Output
Found a number over 15: 16

continue — Skip to the Next Iteration

R Code
numbers = [4, -8, 15, -16, 23]

for n in numbers:
    if n < 0:
        continue   # skip negative numbers entirely
    print(n)
Output
4
15
23

pass — A Placeholder That Does Nothing

pass is a no-op statement. It is used when Python requires a statement syntactically (like inside an empty function or if block) but you are not ready to write the real code yet.

R Code
def todo_function():
    pass   # placeholder - no error, just does nothing yet

for n in [1,2,3]:
    if n == 2:
        pass   # intentionally do nothing special for 2, for now
    else:
        print(n)
Output
1
3
💡 Tip
pass is different from continue: pass does not skip anything, it just satisfies Python's requirement for a statement while genuinely doing nothing. Leaving a function or if body completely empty (with no pass) is a SyntaxError.

Advanced: break with a for...else Clause

🚀 Advanced Example
Python's lesser-known for...else runs the else block only if the loop completed WITHOUT hitting a break - useful for search patterns.
R Code
numbers = [4, 8, 15]
target = 99

for n in numbers:
    if n == target:
        print("Found it!")
        break
else:
    print("Target not found in the list.")
Output
Target not found in the list.

Key Points

  • break exits a loop immediately; continue jumps straight to the next iteration.
  • pass is a placeholder statement that does nothing - it satisfies Python's syntax without changing behaviour.
  • for...else runs its else block only when the loop finishes without hitting break.