1.2.20 · D3Introduction to Programming (Python)

Worked examples — break, continue, pass — when and why

4,057 words18 min readBack to topic

The scenario matrix

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.


Cell A — break: search, target present

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.


Cell B — for...else: target absent

The for...else and while...else else 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".


Cell C — continue: filter one item

The strip below marks each n from 1 to 8: cyan "add" columns feed total, amber "skip" columns are the ones continue drops.


Cell D — continue in a while: the counter trap

The strip below shows both layouts side by side: the top row (increment before continue) terminates; the bottom row (increment after) freezes on an even i forever — the amber "STUCK" marker.


Cell E — degenerate input: empty for, body runs zero times

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 F — degenerate while: condition false immediately

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.


Cell G — nested loops: which loop does break leave?

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 H — nested loops: which loop does continue skip?

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.


Cell J — real-world word problem

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.


Cell K — exam twist: does continue suppress else?

This is the trap that catches most students: they know break 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.


The matrix, filled

Recall Which example covered which cell?

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)


Connections

  • For Loops — cells A, B, C, E, G, H, K run on for iteration.
  • While Loops — cells D and F are the while counter trap and the degenerate while.
  • for...else and while...else — cells B, E, F, K test the else-vs-break rule for both loop kinds.
  • Conditionals (if-elif-else) — every keyword here lives inside an if.
  • Functions and def — cell I stubs a function body with pass.
  • Prime Number Checking — the archetypal while...else + break search (cell B pattern).