break – Exit the Loop Immediately
break immediately terminates the innermost loop. Execution continues with the first statement after the loop.
# 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")
continue – Skip to Next Iteration
continue skips the rest of the current iteration and jumps to the next one. The loop itself continues running.
# Print only odd numbers
for num in range(1, 11):
if num % 2 == 0:
continue # Skip even numbers
print(num)
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.
# 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.
# 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}")
break, continue, pass — Three Different Jobs
These keywords are easy to confuse because they all appear inside loops, but they do completely different things.
| Keyword | Effect |
|---|---|
break | exit the loop entirely, now |
continue | skip to the next iteration |
pass | do 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:
- Loop over numbers 1–20 and
breakas soon as you find the first multiple of 7. - Loop over 1–10 and use
continueto skip even numbers, printing only the odd ones. - Write an empty function body using
passas a placeholder. - In a nested loop,
breakout 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
breakexits the nearest enclosing loop immediately.continueskips the rest of the current iteration and moves to the next.passis a no-op placeholder that does nothing — useful where syntax requires a statement.- In nested loops,
breakandcontinueaffect only the innermost loop. - A loop’s
elseblock runs only if the loop finished without hittingbreak. - 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
breakstatement do? - What is the difference between
breakandcontinue? - What is the purpose of the
passstatement? - In nested loops, which loop does
breakexit? - What is the
for...elseconstruct and how doesbreakaffect it? - How can you break out of multiple nested loops at once?
- When would you use
passin real code?
Related Topics
FAQ
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.
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.
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.
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.

