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 statement; it satisfies Python 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 the outer value, inner value, output, and exit event.
break
break exits the nearest enclosing loop immediately. Code 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, and 2. When n becomes 3, break exits the loop before the print statement.
In nested loops, break exits only the inner loop that contains it.
for row in range(2):
for col in range(3):
if col == 1:
break
print(row, col)
The outer loop still continues with the next row.
continue
continue skips the rest of the current iteration. In a for loop, Python moves to the next iterable value. In a while loop, Python goes back 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 the print is skipped for those iterations.
The exam trap is usually a statement below continue that never runs for some values. In a while loop, that skipped statement might be the update needed to terminate the loop.
pass
pass does nothing. It is a placeholder used where Python syntax requires a statement.
for n in range(3):
if n == 1:
pass
print(n)
This prints 0, 1, and 2. pass does not mean skip the rest of the block. It simply performs no action and execution continues with the next statement.
Loop else
Python allows else on for and while loops. The loop else runs only if the loop finishes normally without break.
for ch in 'abc':
if ch == 'x':
print('found')
break
else:
print('missing')
This prints missing because the loop never breaks. If ch == 'x' had become true and break had executed, the else block would be skipped.
Do not read loop else as the opposite of the loop condition. It is better to read it as no break happened.
Nested-loop trace table
For nested loops, finish the entire inner loop for one outer value before moving to the next outer value. Use columns like these:
| Outer | Inner | Event | Output |
|---|---|---|---|
| 0 | 0 | 00 | |
| 0 | 1 | break inner | none |
| 1 | 0 | 10 | |
| 1 | 1 | break inner | none |
This prevents the common mistake of thinking a break exits all loops. It also makes continue easier 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 breaks. Remember that continue does not end the whole loop; it just moves to the next iteration. Finally, treat pass as a blank line that is syntactically valid, not as a hidden branch-control command.
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=';')