1.2.20 · D5Introduction to Programming (Python)
Question bank — break, continue, pass — when and why

True or false — justify
break exits all loops it is nested inside.
False —
break only leaves the nearest enclosing loop (the red arrow in the figure exits one loop level). An outer loop keeps running; you'd need a flag or a helper function to escape both.continue re-checks the loop condition (in a while) before the next round.
True —
continue follows the dashed orange arrow back to the top of the loop machinery, so a while re-evaluates its condition and a for advances its iterator, exactly as a normal end-of-body would.pass and an empty line are interchangeable.
False for syntax — Python forbids an empty block, so a body needing at least one statement (
if:, def:, for:) needs pass; a blank line there is a syntax error.If a loop hits break, its else clause still runs afterward.
False —
break is precisely what ==suppresses the else== (the red arrow jumps past the else to after the loop). The else runs only when the loop finishes without any break.If a loop hits continue, its else clause is skipped.
False —
continue (orange arrow, back to the top) doesn't count as break. The loop can still finish normally later, so the else runs as usual.Using break inside an if inside a loop breaks out of the if, not the loop.
False —
if is not a loop, so break ignores it entirely and exits the loop. break/continue only ever target loops.Since pass is a no-op, it has zero runtime cost.
False —
pass still compiles to a tiny no-op bytecode that the interpreter executes, so it costs a few CPU cycles (negligible, but not literally zero). The real point is that it changes nothing about control flow — execution simply falls through to the next line (the green arrow in the figure).A for...else with a break inside prints the else message when the item is found.
False — finding the item usually triggers the
break, which skips the else. The else means "search finished, nothing found", so it fires when the item is absent.A break inside try: skips the finally: block as it leaves the loop.
False —
finally always runs before control actually leaves; break is held until finally completes, then the loop exits. Same for continue.Writing break at the top level of a script (not inside any loop) just does nothing.
False —
break/continue outside every loop raise a ==SyntaxError: 'break' outside loop'==. They are only legal syntactically inside a loop body.Spot the error
for x in data:
if x < 0:
continue
else:
pass
process(x)What's redundant here?
The
else: pass does nothing — if x < 0 we already continued, so control only reaches process(x) when x >= 0. The whole else/pass block can be deleted.while True:
line = read()
if line == "quit":
continue # meant to stopWhy does this hang forever?
continue restarts the loop (orange arrow, back to the top) instead of leaving it. On a while True you need ==break== to exit; continue just loops back to read() again.for i in range(5):
if i == 3:
break
print(i)
else:
print("done")Will "done" print?
No — the loop
breaks at i == 3, and break suppresses the loop else. Output is 0 1 2 only.def check(x):
if x > 0:
pass
return "positive"Why does this always return "positive"?
pass does nothing, and the return sits outside the if, so it runs for every input. The if branch is effectively dead code.for i in range(3):
continue
print(i)Why does this print nothing?
continue jumps to the next iteration immediately, so print(i) is unreachable on every pass. Nothing after continue in the body ever runs.for i in range(3):
break
print("hi")What actually happens on the first iteration?
The loop enters iteration
i == 0, executes break as its very first statement, and exits — so print("hi") never runs and the output is empty. It does enter the loop once, but leaves before doing any visible work.result = [x for x in range(5) if x != 2 else continue]Why is this illegal?
A comprehension is an expression, not a loop body, so
break/continue are a SyntaxError inside it. Filtering is done with the trailing if (here if x != 2), never with continue.try:
for x in items:
if bad(x):
break
finally:
cleanup()Does cleanup() run if break fires?
Yes —
finally runs no matter how the try block is left, including via a break that exits the loop. That's exactly what finally is for.Why questions
Why does break "save work" in a search loop?
Once the target is found there's no reason to inspect remaining items, so
break stops the scan early — and it also prevents later matches from overwriting your result.Why prefer continue over wrapping the body in a big if?
continue handles the "skip" case at the top and lets the main logic sit un-indented below, so the important code reads flat instead of buried inside nested conditions.Why does pass exist at all if it does nothing?
Because Python uses indentation for blocks and requires at least one statement where a block is expected;
pass is the legal way to say "a body goes here, but I have nothing to put in it yet".Why is for...else cleaner than a boolean flag for "not found"?
The
else fires exactly when no break happened, encoding "searched everything, found nothing" directly in the language — no extra found = False variable to declare, set, and test.Why do break and continue only affect the innermost loop?
They refer to "the loop I am currently executing". Making them jump across multiple nesting levels would be ambiguous and error-prone, so Python restricts them to the nearest enclosing loop.
Why can't you use continue to skip an item inside a list comprehension?
A comprehension isn't a loop statement — it's a compact expression, so there's no loop body for
continue to jump within. You express "skip" with its own if filter instead.Why does a finally still run when a break leaves the loop?
finally guarantees cleanup on every exit path from the try — normal fall-through, exception, return, break, or continue — so resources are never left dangling regardless of how you leave.Edge cases
What does for x in []: with a break inside do?
The body never runs (empty iterable), so the
break is never reached; the loop finishes normally and any else clause runs.Does continue in the last iteration change anything visible?
It skips the rest of that iteration's body, then the loop ends because there are no more items. If an
else exists (and no break occurred), it still runs.while False: break — what happens to the loop and its else?
The condition is false, so the body (and the
break) never execute; the loop is considered completed normally, so a trailing else runs.Can you put pass inside a loop body as the only statement?
Yes —
for x in data: pass is legal and simply iterates doing nothing (sometimes used to exhaust an iterator or as a deliberate stub).Nested loop: inner loop breaks — does the outer loop continue?
Yes.
break leaves only the inner loop; the outer loop proceeds to its next iteration as if the inner loop finished.If both a continue and later a break could run in one iteration, which wins?
Whichever executes first ends that iteration —
continue immediately jumps to the next round, so any break written below it on the skipped path never runs.What happens if you write continue inside a generator expression like (x for x in data if continue)?
A
SyntaxError — like comprehensions, generator expressions are expressions with no loop body, so break/continue are simply not allowed; use an if filter.Inside try: ...; continue with a finally:, does the next iteration start before or after finally?
After —
finally runs first (cleanup completes), then continue proceeds to the next iteration. Control is never handed on until finally finishes.Connections
- For Loops — where
break/continueact on the iterator. - While Loops — condition is re-checked after
continue. - for...else and while...else —
breakis the switch that suppresseselse. - Conditionals (if-elif-else) — these keywords live inside
if, but only target loops. - Functions and def —
passas a stub body. - Prime Number Checking — the classic
while...else+breaktrap.