1.2.20 · D2Introduction to Programming (Python)

Visual walkthrough — break, continue, pass — when and why

2,309 words10 min readBack to topic

We build from zero. First we must agree on what a loop even is as a picture — before any keyword enters.


Step 1 — What a loop looks like with NO special keywords

WHAT. A plain loop is a repeating cycle: ask a question → if yes, run the body → come back and ask again → when the answer is no, leave.

WHY start here. You cannot understand what break "interrupts" until you can see the uninterrupted motion. Every keyword later is just a modification of this one loop.

PICTURE. Look at the figure. The train starts at the check (pale-yellow diamond). If the check says "more items left?" is yes (blue arrow), it enters the body and runs every line top to bottom. The pink arrow at the bottom carries it back up to the check. When the check finally says no, the train exits along the bottom to the line after the loop.

Figure — break, continue, pass — when and why

No keyword yet. Just a train going round. Hold this image — everything else is a small edit to it.


Step 2 — break: cut the track, send the train straight to the exit

WHAT. break is a signal that says stop the whole ride right now — no more checks, no more iterations.

WHY this and not just letting the loop finish? Sometimes the reason to loop disappears mid-body. You are searching a shelf of books; the instant you find your book, checking the rest is wasted effort. break lets the body talk back to the check and say "we're done."

PICTURE. Trace the red arrow. The train is inside the body when it hits break. Instead of continuing down to the remaining lines, it is yanked off the loop entirely — it does not return to the check, it does not run the lines below break. It lands directly on the first line after the loop.

Figure — break, continue, pass — when and why

Step 3 — continue: send the train back to the check, skip the rest of this pass

WHAT. continue says abandon this one pass, but keep the loop alive — jump straight back up to the check.

WHY a separate keyword from break? Because "I don't care about this item" is a totally different wish from "I don't care about any remaining item." continue throws away the current iteration only; the loop keeps going.

PICTURE. Follow the blue arrow. The train hits continue partway down the body. The lines below continue are skipped (just like break) — but instead of exiting, the train loops back up to the check (pink return path) and starts the next iteration. The exit is not taken.

Figure — break, continue, pass — when and why

Step 4 — pass: a signal that changes NOTHING

WHAT. pass is a station sign that reads "do nothing here." The train reads it and just keeps rolling down to the next line — no jump, no exit.

WHY does a "do nothing" keyword need to exist at all? Because Python's grammar demands that a block (after if:, for:, def:, class:) contain at least one statement. An empty block is a syntax error — the program won't even start. pass is the smallest legal filler: it satisfies the grammar while doing zero work.

PICTURE. Compare the two trains. On the pass track the train reads the sign and continues straight down to the next line — it behaves exactly as if the line weren't there. On the continue track (shown faded for contrast) the train would have jumped back up. This is the whole difference, drawn.

Figure — break, continue, pass — when and why

Step 5 — The else clause: a reward for finishing WITHOUT break

WHAT. A loop can have an else: block. It runs only if the loop reached its exit naturally — i.e. the check finally said "no" — and never if a break fired.

WHY tie else to break? Because the most common loop is a search, and a search has exactly two endings: "I found it" (you break) or "I scanned everything and found nothing" (you finished normally). The else is the natural home for the "found nothing" case — no flag variable needed.

PICTURE. Two trains. The top train never hits break; the check eventually says "no", it slides through the yellow else box, then exits. The bottom train hits break; it is yanked out and jumps over the else box entirely (red dashed skip).

Figure — break, continue, pass — when and why

Step 6 — Edge & degenerate cases (the train still needs a picture)

WHAT. Three cases people trip over: a loop whose body never runs, a break/continue inside nested loops, and continue interacting with else.

WHY these deserve their own step. The contract: the reader must never meet a scenario we didn't show. These three break naive intuition.

PICTURE.

  • (a) Empty loop — if the check is "no" on the very first ask, the body never runs. Since no break could fire, the else runs immediately.
  • (b) Nested loopsbreak and continue obey only the nearest enclosing loop. The inner train's break drops it into the outer body, not out of both. The outer train keeps going.
  • (c) continue + elsecontinue skips passes but never ends the loop, so the loop can still finish naturally and the else still fires.
Figure — break, continue, pass — when and why

The one-picture summary

Here is the entire walkthrough compressed into one track diagram. Memorise this, and you can regenerate every rule above.

Figure — break, continue, pass — when and why
  • pass → straight down (no effect on the train's route).
  • continue → curve back up to the check (skip rest of body, keep looping, else can still run).
  • break → cut off the track to the exit (skip rest of body, skip all future passes, skip else).
  • else → the box just before the exit — visited only if the train reached it naturally.
Recall Feynman: the whole story in plain words

Picture a train on a circular track. At the top is a gate that asks "any items left?" If yes, the train rolls through all the carriages of the body, then curves back to the gate. When the gate says no, the train leaves along the exit — but first it may stop at a little else platform. Now the three signals you can plant inside the carriages: pass is a blank sign — the train reads it and just rolls to the next carriage. Nothing changes; it only exists because the track rules forbid a completely empty carriage. continue is a "return to gate now" signal — the train skips the remaining carriages and curves back to the gate for the next round. The else platform is still reachable later, because the loop hasn't actually ended. break is an "abandon ship" signal — the track is cut and the train shoots straight to the exit. It skips the rest of the carriages, and every future round, and it flies right past the else platform. That's the whole thing: pass = do nothing, continue = go around again, break = leave for good.


Connections

  • ← Back to the parent topic
  • For Loops — the check here is "iterator exhausted?"
  • While Loops — the check is your boolean condition, re-tested after continue.
  • for...else and while...else — the else-platform rule drawn in Steps 5–6.
  • Conditionals (if-elif-else) — every signal lives inside an if that decides whether to fire it.
  • Functions and defpass as a legal empty stub.
  • Prime Number Checking — the while...else + break pattern of Step 5.