Course topics

By WebNest Studio

Python Tutorial

break, continue and pass in Python

Sometimes a loop should stop early, skip an item, or do nothing at all for now. Python gives you three small statements for this: break exits the loop, continue skips to the next iteration, and pass is a placeholder that does nothing.

This lesson explains each statement, the difference between break and continue, how they behave in nested loops, and where pass is useful.

break

break immediately ends the innermost loop; execution continues after the loop, and any loop else block is skipped. Typical uses: stop searching once you find something, exit a while True loop when the user enters "quit".

continue

continue skips the rest of the current iteration and jumps to the next one. It keeps loop bodies flat: filter out invalid items at the top with if bad: continue instead of wrapping the rest in an if.

pass

pass does nothing. Python requires at least one statement in a block, so pass is used as a placeholder for functions or classes you will implement later, empty exception handlers (use rarely!), or minimal class definitions. ... (Ellipsis) is often used the same way in stubs.

break vs continue in Nested Loops

Both affect only the innermost loop. To exit several loops at once, move the loops into a function and return, or use a flag variable.

Examples

break and continue side by side

Python
print("break:")
for n in range(1, 10):
    if n == 5:
        break
    print(n, end=" ")
print()

print("continue:")
for n in range(1, 10):
    if n % 3 == 0:
        continue
    print(n, end=" ")
print()
Output
break:
1 2 3 4
continue:
1 2 4 5 7 8

A menu loop with while True and break

Python
commands = ["add", "list", "oops", "quit", "never reached"]
cart = []
for cmd in commands:
    if cmd == "quit":
        print("Bye!")
        break
    if cmd not in ("add", "list"):
        print(f"Unknown command: {cmd}")
        continue
    if cmd == "add":
        cart.append("item")
    print(cmd, "->", cart)
Output
add -> ['item']
list -> ['item']
Unknown command: oops
Bye!

pass as a placeholder, and exiting nested loops with return

Python
class PaymentGateway:
    pass                      # to be implemented later

def todo():
    pass

def find_pair(numbers, target):
    for i, a in enumerate(numbers):
        for b in numbers[i + 1:]:
            if a + b == target:
                return a, b      # leaves both loops
    return None

print(type(PaymentGateway()).__name__, todo())
print(find_pair([2, 7, 11, 15], 18))
print(find_pair([1, 2, 3], 100))
Output
PaymentGateway None
(7, 11)
None

Common Mistakes

  • Expecting break to exit all nested loops — it only exits the innermost one.
  • Using continue in a while loop before incrementing the counter, creating an infinite loop.
  • Using except: pass to silence errors, hiding real bugs.
  • Confusing pass (do nothing, keep going) with continue (skip to the next iteration).

Key Points to Remember

  • break exits the innermost loop and skips its else clause.
  • continue skips the rest of the current iteration.
  • pass is a no-op placeholder required where a statement is syntactically needed.
  • Use a function with return to leave several nested loops at once.

Practice the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.