Break, Continue, Pass, Loop Else, and Nesting
Key Takeaways
- break exits only the nearest enclosing loop and prevents that loop's else clause from running.
- continue skips the rest of the current iteration and moves to the next loop condition or next iterable value.
- pass is a do-nothing placeholder; it satisfies syntax but does not skip later statements in the block.
- A loop else clause runs only when the loop finishes normally without executing break.
- Nested-loop PCEP questions should be traced with separate columns for outer value, inner value, output, and exit event.
break
break exits the nearest enclosing loop immediately; statements after the loop may still run, but the current loop body stops.
for n in range(5):
if n == 3:
break
print(n)
This prints 0, 1, 2. When n becomes 3, break exits before the print.
In nested loops, break exits only the inner loop that contains it; the outer loop continues.
for row in range(2):
for col in range(3):
if col == 1:
break
print(row, col)
The outer loop advances to the next row after each inner break. Python has no labeled break, so to leave both loops you need a flag or a function return.
continue
continue skips the rest of the current iteration. In a for loop Python moves to the next iterable value; in a while loop it returns to the condition check.
for n in range(5):
if n % 2 == 0:
continue
print(n)
This prints 1 and 3. Even numbers hit continue, so their print is skipped. The dangerous version is a while loop whose counter update sits below continue — that update is skipped and the loop can run forever.
pass
pass does nothing. It is a placeholder where Python's grammar requires a statement but you have no action yet.
for n in range(3):
if n == 1:
pass
print(n)
This prints 0, 1, 2. pass does not mean skip the rest of the block; it performs no action and execution continues to the next statement. Contrast the three:
| Keyword | Effect |
|---|---|
break | leave the loop entirely |
continue | jump to the next iteration |
pass | do nothing, fall through to the next line |
Loop else
Python allows else on both for and while. The loop else runs only if the loop finished without executing break.
for ch in 'abc':
if ch == 'x':
print('found')
break
else:
print('missing')
This prints missing because the loop never breaks. Had a break fired, the else would be skipped. Read loop else as "no break happened," not as the negation of the loop condition. This is a textbook search pattern: break on a hit, else to report no hit.
Nested-loop trace table
Finish the entire inner loop for one outer value before moving on. Use columns for each piece.
| Outer | Inner | Event | Output |
|---|---|---|---|
| 0 | 0 | 00 | |
| 0 | 1 | break inner | none |
| 1 | 0 | 10 | |
| 1 | 1 | break inner | none |
This prevents the most common error: believing a single break exits all loops. It also makes continue easy to follow because you can mark exactly which iteration was skipped.
Exam traps
Check indentation before deciding whether an else belongs to an if or to a loop. Remember that break suppresses only the else attached to the loop it leaves. Remember that continue does not end the loop; it advances to the next iteration. Treat pass as a syntactically valid blank line, not a hidden control command. And recall that break/continue outside any loop raise a SyntaxError.
continue in a while loop is the dangerous case
In a for loop, continue is safe because the loop variable advances automatically from the iterable. In a while loop, continue returns to the condition test and can skip the very statement that updates the counter.
i = 0
while i < 5:
if i == 2:
continue # i is never incremented again
print(i)
i += 1
When i reaches 2, continue jumps back to the test with i still 2, looping forever. The fix is to update i before continue. Recognizing this infinite-loop pattern is a frequent PCEP exam item.
break exits one level only
Because Python has no labeled break, leaving nested loops requires a flag or wrapping the loops in a function and using return.
found = False
for row in grid:
for cell in row:
if cell == target:
found = True
break
if found:
break
The inner break leaves the inner loop; the outer if found: break then leaves the outer loop. The exam wants you to know a single break cannot escape both at once.
Loop else as a search reporter
The for/else idiom is the canonical "search and report" pattern. The else says no break happened, meaning nothing was found.
nums = [2, 4, 6]
for n in nums:
if n % 2 == 1:
print('odd found')
break
else:
print('all even')
No odd value exists, so the loop completes without breaking and the else prints all even.
Quick-reference behavior table
| Construct | In a for loop | In a while loop | Effect on loop else |
|---|---|---|---|
break | leaves the loop now | leaves the loop now | else is skipped |
continue | next iterable value | back to condition (skips updates below it) | else still possible |
pass | no effect | no effect | no effect |
| normal end | else runs | else runs | else runs |
Final tracing routine
For nested or control-heavy snippets: complete the inner loop fully for each outer value; mark each break/continue/pass event in its own row; verify which loop a break actually exits; and only after the trace decide whether any loop else runs. This disciplined approach is what lets you finish the Control Flow questions — the largest slice of the exam — accurately within the time limit.
What is printed by this code? for n in range(3): if n == 1: break else: print('done') print('end')
What is printed by this code? for n in range(4): if n % 2 == 0: continue print(n, end='')
What is printed by this code? for a in range(2): for b in range(3): if b == 1: break print(a, b, end=';')
Which statement correctly describes the pass keyword?