Think of a loop as a machine that can be interrupted. The situations differ along a few axes: which keyword, where the interrupt sits (start / middle / end of body), is there an else (and for both for and while), is the loop nested, and degenerate cases (empty data, or a condition that is false immediately). Below is every cell we cover.
#
Cell (case class)
Keyword
Twist being tested
A
Search, target present
break
stop early, keep the index
B
for...else, target absent
break + else
else fires when no break
C
Filter/skip an item
continue
rest of body skipped, loop lives on
D
continue in a while — advance-the-counter trap
continue
forgetting i += 1 = infinite loop
E
Empty for (degenerate)
all three
body runs zero times
F
while condition false immediately (degenerate)
while...else
body zero times, else still runs
G
Nested loops — which loop does break leave?
break
only the innermost exits
H
Nested loops — which loop does continue skip?
continue
only the innermost iterates
I
Placeholder body
pass
syntax needs a statement, logic is empty
J
Real-world word problem
break+continue
vending machine / stock check
K
Exam twist: continue vs else
continue
does skipping suppress else? (No)
We hit every cell A–K below. Each cell gets its own annotated execution strip (a picture of the loop's rounds) so you can see the control flow, not just read it.
The strip below is a picture of the loop's rounds. Each column is one iteration (i, x). A cyan box = the body ran and we moved on; the amber octagon with the word "BREAK" = where we bail out. Greyed columns are iterations that never happen because we already left.
The for...else and while...elseelse clause is the mirror image of break: it runs only if the loop finished without breaking.
Walk the flowchart below before the example — it is the map for every else case on this page. Read the labels, not the colours (each box is also shaped and lettered so colour-blind readers get the same information):
Start at the top-left rectangle labelled "check condition". If items remain, follow the arrow marked "item" right into the rectangle "run body B" (recall: B = the loop body).
Inside the body two things can happen. A continue (arrow labelled "continue") jumps to the rectangle "next item" and loops back to the condition. A break (the arrow labelled "break") leaves along the path to the octagon "break → exit" and skips the else entirely.
If the condition ever runs out of items with no break, you drop down the arrow labelled "done / no break" to the rectangle "loop ends naturally", and only then does the rounded box "else clause" run.
The single rule the picture encodes: the "else clause" box is reachable only along the path labelled "no break".
The strip below shows both layouts side by side: the top row (increment beforecontinue) terminates; the bottom row (increment after) freezes on an even i forever — the amber "STUCK" marker.
The strip below shows an execution timeline with no columns at all — a visual reminder that "zero rounds" is a real, legal outcome — with the amber else box firing at the end.
Cell E used a for. The while version of "the body never runs" is a condition that is false the very first time it is checked — and we test what while...else does then.
The strip below shows the very first condition-check failing (amber "False" tag on round 0), the body strip empty, and else still firing.
The grid below is the map of every (r, c) pair: cyan cells run hits += 1, the amber octagon in each row is where the inner loop breaks, and grey cells are unreached because that row already broke.
Cell G showed break only leaves the inner loop. continue is the same story: it skips only the rest of the inner iteration, and the inner loop keeps iterating; the outer loop is untouched.
The grid below is the twin of cell G's: same 3×3, but now the diagonal cell in each row is an amber "skip" (that one c is passed over) and every other cell still counts — no row is cut short.
The strip below contrasts the three keywords on the same branch: break exits (amber octagon), continue jumps to next round (amber curved arrow), and pass is a hollow box that changes nothing — execution falls straight through to the next line.
The strip below walks the snack list left to right: amber "skip (too dear)" columns are continued, the first affordable snack is the cyan "BUY" column, and everything after it is greyed "unreached" because we break.
This is the trap that catches most students: they knowbreak cancels the else, and wrongly assume continue does too. It does not. The strip below shows the loop running to natural completion even though a continue fired mid-way — so the amber else box still lights up.
A::: Ex A (first even, break)
B::: Ex B (all odd, for...else fires)
C::: Ex C (skip multiples of 4, continue)
D::: Ex D (while + continue, increment-first)
E::: Ex E (empty for, body zero times)
F::: Ex F (while condition false immediately, while...else)
G::: Ex G (nested break, inner only)
H::: Ex H (nested continue, inner only)
I::: Ex I (pass as stub)
J::: Ex J (vending machine, break+continue interact)
K::: Ex K (continue does NOT suppress else)