Advertisement
🔀 Control Flow

Python break, continue, and pass – Loop Control Statements

break, continue, and pass are loop control statements that change the normal flow of a loop. break exits the loop completely, continue skips the rest of the current iteration and moves to the next, and pass does nothing — it's a placeholder.

⏱️ 18 min read 🎯 Beginner 📅 Updated 2026

break – Exit the Loop Immediately

break immediately terminates the innermost loop. Execution continues with the first statement after the loop.

Python
# Find first even number and stop
for num in [1, 3, 7, 8, 9, 10]:
    if num % 2 == 0:
        print(f"Found first even: {num}")
        break
    print(f"{num} is odd")
print("Loop ended")
▶ Output
1 is odd 3 is odd 7 is odd Found first even: 8 Loop ended

continue – Skip to Next Iteration

continue skips the rest of the current iteration and jumps to the next one. The loop itself continues running.

Python
# Print only odd numbers
for num in range(1, 11):
    if num % 2 == 0:
        continue    # Skip even numbers
    print(num)
▶ Output
1 3 5 7 9
Advertisement

pass – The No-Op Placeholder

pass does nothing. It's used when Python requires a statement syntactically but you don't want to execute anything — useful for stubs, empty classes, and empty except blocks.

Python
# Stub function during development
def calculate_tax(income):
    pass  # TODO: implement later

# Empty class
class Employee:
    pass

# Suppress specific exceptions
try:
    result = 10 / 0
except ZeroDivisionError:
    pass  # We know this can happen, intentionally ignore

break and continue in Nested Loops

Important: break and continue only affect the innermost loop they are in. To break out of multiple nested loops, use a flag variable or structure your code differently.

Python
# break only exits the inner loop
for i in range(3):
    for j in range(3):
        if j == 1:
            break   # Exits inner loop only
        print(f"({i},{j})")

# Flag to break outer loop too
found = False
for i in range(5):
    for j in range(5):
        if i * j > 6:
            found = True
            break
    if found:
        break
print(f"Stopped at i={i}, j={j}")
▶ Output
(0,0) (1,0) (2,0) Stopped at i=2, j=4

break, continue, pass — Three Different Jobs

These keywords are easy to confuse because they all appear inside loops, but they do completely different things.

KeywordEffect
breakexit the loop entirely, now
continueskip to the next iteration
passdo nothing (a placeholder)
for n in numbers:
    if n < 0:
        continue        # skip negatives, keep looping
    if n == target:
        break           # found it — stop looping
    process(n)

def todo():
    pass                # empty body placeholder (avoids SyntaxError)

The loop-else clause

for x in items:
    if x == target:
        break
else:                   # runs ONLY if the loop finished WITHOUT break
    print("not found")

Key distinctions: break abandons the whole loop; continue just skips the rest of the current pass. pass is unrelated to loops — it's a syntactic "do nothing" you use where Python requires a statement but you have none yet (an empty function, class, or if branch). The lesser-known for...else: the else runs only if the loop completed without hitting break — perfect for "search and report if not found" without a flag variable.

🏋️ Practical Exercise

Use each loop control statement:

  1. Loop over numbers 1–20 and break as soon as you find the first multiple of 7.
  2. Loop over 1–10 and use continue to skip even numbers, printing only the odd ones.
  3. Write an empty function body using pass as a placeholder.
  4. In a nested loop, break out of the inner loop and observe that the outer loop continues.

🔥 Challenge Exercise

Write a program that searches a 2D grid (list of lists) for a target value. Use nested loops and break to stop searching the moment you find it, printing the row and column. Use a for...else clause to print “not found” if the loops complete without a break. Bonus: refactor the inner break into a flag or a helper function that returns early.

📋 Summary

  • break exits the nearest enclosing loop immediately.
  • continue skips the rest of the current iteration and moves to the next.
  • pass is a no-op placeholder that does nothing — useful where syntax requires a statement.
  • In nested loops, break and continue affect only the innermost loop.
  • A loop’s else block runs only if the loop finished without hitting break.
  • To exit multiple loops, use a flag, a function with return, or restructure the logic.

Interview Questions on break, continue, and pass

  • What does the break statement do?
  • What is the difference between break and continue?
  • What is the purpose of the pass statement?
  • In nested loops, which loop does break exit?
  • What is the for...else construct and how does break affect it?
  • How can you break out of multiple nested loops at once?
  • When would you use pass in real code?

FAQ

What is the difference between break and continue? +

break stops the loop entirely and moves on to the code after it. continue only skips the remaining statements in the current iteration and jumps to the next iteration of the same loop.

Why would I ever use pass? +

pass fills a spot where Python’s syntax requires a statement but you have nothing to do yet — an empty function or class stub, an except block that intentionally ignores an error, or a branch you plan to implement later.

How do I break out of two nested loops at once? +

Python has no labeled break. Common solutions: set a flag and check it in the outer loop, move the loops into a function and return, or use itertools.product to flatten the loops into one.

Does break skip the loop’s else block? +

Yes. A loop else runs only when the loop finishes normally. If break fires, the else is skipped — which is exactly why for...else is handy for search-and-report patterns.