Steering a loop mid-flight: break to leave early, continue to skip one pass, while True + break for input menus — and the loop's own else clause.
01
Learning objectives
Use break to exit a loop immediately (search loops, first-match problems).
Use continue to skip the rest of one pass without ending the loop.
Write the while True + break input pattern safely (a guaranteed exit path).
State that break exits only the innermost loop and that else runs only when no break fired.
Trace loops containing break/continue, including which lines get skipped.
02
Lesson notes
Sometimes the loop's own condition is too blunt an instrument: you want to stop the moment something is found, or skip one awkward item without ending the sweep. Python gives every loop two steering levers. break abandons the loop immediately — execution jumps to the first line after the loop body. continue abandons only the current pass — execution jumps back to the condition (while) or to the next item (for), skipping whatever remained below it in the body.
break is the natural tool of search. Looking for the first number above 100 in a list? Once found, checking the remaining items is wasted work — break says 'we have our answer'. The classic trace question: which lines run after the break fires? None of them — the loop is over. Equally examinable is the scope rule: break exits only the innermost loop containing it; in a nested pair, the outer loop keeps running (and will likely start the inner one again).
continue is the tool of filtering: process the good items, skip the bad ones with one early jump. Printing only odd numbers in 1-10: when n % 2 == 0, continue jumps straight to the next n — the print below never sees even numbers. The subtle trap: in a while loop, a continue placed BEFORE the counter update skips that update — reinventing the infinite loop in one careless line. In for loops this cannot happen (the loop variable advances itself), which is one more reason Pythonists prefer for.
The pair powers Python's most recognisable idiom, the while True + break loop: the condition is deliberately always-True, and the loop ends only through break when a real exit condition appears. Menus and input validation use it because the exit test can sit anywhere it reads best — usually right after the input. The one safety rule: a while True loop MUST contain a break reachable on some input path, or it can never end.
Last, the obscure-but-examinable loop else: a while or for loop may carry its own else: block, which runs only when the loop finished without firing break — Python's built-in 'search failed' hook. for n in nums: if n == target: break else: print("not found") prints 'not found' exactly when the whole list was scanned without a match. Rarely used in the wild, but a favourite 2-mark question — and conceptually it is just 'break skipped, else earned'.
03
Worked code examples
break: stop at the first match
The loop stops the moment 108 qualifies — 250 is never checked. After break, control lands on the print below the loop; the loop's own range is abandoned.
found = None
for n in [7, 13, 42, 108, 250]:
print("checking", n)
if n > 100:
found = n
break
print("First above 100:", found)
continue jumps past the rest of THIS pass only — evens are skipped, odds still print (note the trailing space the end=" " leaves). The census uses the same jump to count only the s characters.
for n in range(1, 11):
if n % 2 == 0:
continue # jump to next n
print(n, end=" ")
print()
word = "mississippi"
count = 0
for ch in word:
if ch != "s":
continue
count += 1
print("s count:", count)
Output
1 3 5 7 9
s count: 4
while True + break: the menu pattern (with loop else)
The program is fed jump, then stop. The always-True condition delegates the exit to break. Reaching break skips the loop's else; if the loop had ever ended by exhausting normally, else would print 'ended without break'. Either way, menu closed prints once.
while True:
cmd = input()
if cmd == "stop":
break
print("doing:", cmd)
else:
print("ended without break")
print("menu closed")
Output
doing: jump
menu closed
04
Key terminology
break
Exits the innermost loop immediately; execution continues after the loop.
continue
Skips the rest of the current pass and jumps to the next iteration.
while True + break
The idiom of an explicitly endless loop whose real exit conditions live in break statements.
loop else
An else block on a loop that runs only when the loop ended WITHOUT break.
early exit
Leaving a loop as soon as the answer is known, instead of scanning the remaining items.
05
Common misconceptions
Common mistake
break exits ALL the loops around it.
Correct understanding
It exits only the innermost loop containing it. In nested loops the outer loop continues its passes.
Common mistake
continue ends the loop.
Correct understanding
continue skips only the remainder of the current pass; the loop itself keeps going.
Common mistake
A loop's else runs after every normal loop.
Correct understanding
The loop else runs only when NO break fired — its meaning is 'the loop completed without early exit'.
06
Check your understanding
1. for n in [1, 2, 3, 4]:
if n == 3:
break
print(n)
What prints?
2. What does continue do inside a for loop?
3. In a nested pair of loops, a break inside the inner loop: