1.2.20 · D4Introduction to Programming (Python)

Exercises — break, continue, pass — when and why

2,380 words11 min readBack to topic

Before we start, one shared mental picture of what a loop is, so every trace below has something to point at.

Figure — break, continue, pass — when and why

Level 1 — Recognition

You just have to read code and say what prints. No design yet.

Recall Solution L1.1

break. "Stop the whole job right now" = leave the loop entirely. continue would only skip the current item and keep scanning; pass would do nothing at all. Look at figure s01: break is the arrow that leaves the cycle.

Recall Solution L1.2

continue skips the print only for i == 2. Every other value prints. Output:

0
1
3
4
Recall Solution L1.3

pass does nothing each cycle, so the loop simply runs to completion. The loop variable c keeps its last value, "h" then "i", ending at "i". Output:

i

Level 2 — Application

Now you write a small piece to hit a stated goal.

Recall Solution L2.1
total = 0
for n in range(1, 11):
    if n % 4 == 0:
        continue          # skip 4 and 8
total += n            # (this line is inside the loop)

Corrected, properly indented:

total = 0
for n in range(1, 11):
    if n % 4 == 0:
        continue
    total += n
print(total)

Skipped values: . Kept: . Total = 43.

Recall Solution L2.2
nums = [3, 8, 2, 15, 6]
for x in nums:
    if x > 10:
        print(x)
        break
else:
    print("none")

15 is the first value over 10, so it prints and break fires. Because a break happened, the else is skipped. Output:

15

Level 3 — Analysis

Here you must trace carefully, tracking why each branch is taken.

Recall Solution L3.1

Walk the cycle for each i:

  • i = 0: even → continue → nothing printed.
  • i = 1: odd, not > 3 → prints 1.
  • i = 2: even → continue.
  • i = 3: odd, not > 3 (3 is not > 3) → prints 3.
  • i = 4: even → continue (the break check is below the continue, so it's never reached for even numbers).
  • i = 5: odd, and 5 > 3break. Loop ends.

Output:

1
3

Note the subtlety: i = 4 would have triggered break if the order were reversed — but continue fires first for even numbers, so 4 never reaches the break test.

Recall Solution L3.2

break exits only the nearest enclosing loop — the inner b loop. The outer a loop keeps going.

  • a = 0: b = 0 prints 0 0; b = 1break (inner loop ends).
  • a = 1: b = 0 prints 1 0; b = 1break.
  • a = 2: b = 0 prints 2 0; b = 1break.

Output:

0 0
1 0
2 0

Level 4 — Synthesis

Design a loop that combines the keywords with real logic.

Recall Solution L4.1
n = 21
i = 2
while i * i <= n:
    if n % i == 0:
        print("not prime")
        break
    i += 1
else:
    print("prime")

Trace: i = 2, 2*2 = 4 <= 21, 21 % 2 = 1 → not a divisor, i becomes 3. i = 3, 9 <= 21, 21 % 3 = 0 → divisor found! Prints "not prime", break. The else is skipped. Output:

not prime

(See Prime Number Checking for the full pattern and why i*i <= n is enough — no divisor above can exist without a matching one below it.)

Recall Solution L4.2
def handle(event):
    if event == "quit":
        print("bye")
    elif event == "save":
        pass            # TODO: implement saving
    else:
        print("unknown")
  • handle("save") → the pass branch runs, does nothing, function returns silently. Prints nothing.
  • handle("open")"open" matches neither → else → prints "unknown".

pass here keeps the elif block syntactically legal while you defer its logic — see Functions and def and Conditionals (if-elif-else).


Level 5 — Mastery

No scaffolding. Reason about behaviour you have to reconstruct from principles.

Recall Solution L5.1

No value in [1, 3, 5, 7] is even, so the if never triggers, break never fires, and found_even stays False. The continue on the last line simply sends us to the next iteration each time — it does not suppress the else. The loop reaches its natural end with no break, so the else runs. Output:

all odd
False

Key insight: only break suppresses else. continue is irrelevant to the else decision — see for...else and while...else.

Recall Solution L5.2
data = [4, 9, 16, 25, 30, 36]
for v in data:
    if v % 2 != 0:
        continue                  # skip odd numbers early
    root = int(v ** 0.5)
    if root * root == v:          # v is a perfect square
        print(v)
        break
else:
    print("none")

Trace:

  • 4: even, int(4**0.5)=2, 2*2=4 ✓ → prints 4, break.

The very first element already qualifies. Output:

4

The continue keeps the even-filter at the top so the perfect-square test reads cleanly without nesting — exactly the design goal from the parent note.


Recall Feynman recap: the three in one sentence each
  • break = "leave the building." Nearest loop only.
  • continue = "skip this room, go to the next." Loop keeps running; else still fires at the end.
  • pass = "stand here doing nothing, then carry on." Purely for syntax.

Connections

  • For Loops — every trace above is a for cycle.
  • While Loops — L4.1 uses a while with re-checked condition.
  • for...else and while...else — L2.2, L4.1, L5.1 hinge on the else rule.
  • Conditionals (if-elif-else) — the keywords live inside if/elif.
  • Functions and def — L4.2 stubs a function body.
  • Prime Number Checking — L4.1 is the trial-division pattern.
  • Hinglish version →