Breaking Out of Loops: Using break and continue
A loop's body does not control termination by itself. Termination depends on the loop condition and on whether the values used by that condition move toward a stopping point.
Try it: while Loops, break and continue
How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.
How it works
- Evaluate the condition. If it is False, skip the body and continue after the loop.
- If it is True, run the body from the top, then go back and evaluate the condition again.
- continue skips the rest of the body and goes straight back to the condition check.
- break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
- If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.
Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.
Loading the simulation…
Why Loops Keep Running
An infinite loop never terminates because its condition never becomes false. The important question is therefore not simply whether a loop contains repeated work. The important question is whether the condition controlling the loop can eventually become false.
A loop's body does not control termination by itself. Termination depends on the loop condition and on whether the values used by that condition move toward a stopping point.
Following Control Flow
Use the diagram as a tracing model. A normal iteration returns to the loop condition after the loop body completes. A break provides an exit from a loop before the condition becomes false. The source pack identifies break as a possible event that intentionally ends a while True loop. The source pack does not provide a detailed definition of continue, so the key diagnostic lesson here is to trace every control-flow path and ask whether it can reach a terminating condition or an intentional exit.
Finite Progression
A terminating loop has three connected features: a finite starting value, a condition that checks that value, and code inside the loop that moves the value toward a stopping point. If the condition eventually becomes false, the loop terminates. The body can perform many different tasks, but those tasks matter for termination only when they affect the values used by the condition.
| Loop structure | What the condition depends on | Termination risk |
|---|---|---|
| Finite iteration | A finite variable that changes toward a stopping point | The condition can become false |
| No iteration variable | No changing value is directing the loop toward an end | The loop can run indefinitely |
| Constant True condition | A condition that is always true | The condition never becomes false |
The relationship between the condition and the values it checks determines whether the loop can terminate.
Tracing a finite iteration
A loop begins with a finite iteration variable. The loop condition checks that variable, and the loop body changes it toward a stopping point. What should you inspect to decide whether the loop terminates?
- 1Find the starting value
Confirm that the loop has a finite starting value rather than no iteration variable at all.
- 2Find the condition
Identify the condition that checks the iteration variable. This condition is the direct authority on whether another iteration begins.
- 3Trace the update
Follow the change made to the variable inside the loop. The change must move the value toward the stopping point used by the condition.
- 4Check the endpoint
Decide whether the variable can reach a value that makes the condition false. If it can, the loop has a terminating path.
A finite iteration is structurally safe when its condition checks a changing value that can reach a stopping point.
The while True Pattern
The constant True in a while condition creates an infinite loop because the condition itself never becomes false. Each time the condition is checked, it produces the same true result, so the loop keeps returning to its body unless another intentional exit is provided.
Runaway Loop Diagnosis
- 1Start with the loop condition. Write down exactly what must become false for normal termination.
- 2Identify every variable or value used by that condition.
- 3Trace whether the loop body changes those values.
- 4Check whether each control-flow path reaches a condition-changing update or an intentional break.
- 5If the condition is while True or there is no iteration variable, locate the specific event that is supposed to end the loop.
- 6If no terminating path is clear, treat the loop as a likely bug and stop examining the repeated path until its exit logic is understood.
Common mistake Assuming that work inside the loop automatically makes the loop terminate.
What it looks likeA loop body performs repeated tasks, but the value checked by the condition never changes.
Why it failsThe loop body does not control termination by itself. The condition must eventually become false, or an intentional exit must be reached.
Do this insteadTrace the values used by the condition and verify that the body moves them toward a stopping point.
Common mistake Treating the presence of a variable as proof that the loop is finite.
What it looks likeA loop has a variable, but that variable is not changed toward the stopping point.
Why it failsA terminating loop needs a finite starting value, a condition that checks it, and code that moves it toward the end.
Do this insteadCheck the complete relationship among the starting value, condition, and update.
Common mistake Reading while True without asking how it exits.
What it looks likeA loop uses the constant True, but no specific event or break statement provides an exit.
Why it failsTrue never becomes false, so the condition cannot provide normal termination.
Do this insteadLocate the intended event or break statement. If no exit is clear, investigate the loop as a likely bug.
Common mistake Assuming every infinite loop is accidental.
What it looks likeA programmer uses while True intentionally until user input or another specific event occurs.
Why it failsInfinite loops can be deliberate when a specific event controls their exit.
Do this insteadDistinguish an intentional event-controlled loop from a loop whose exit path is missing or unclear.
Practice the Trace
Consider a loop whose condition checks a finite iteration variable. The variable begins with a finite value, but one path through the loop does not move it toward the stopping point. Identify the two questions you should ask before deciding whether the loop terminates.
Hints
- First identify the value checked by the condition.
- Then trace whether every repeated path changes that value toward a point that makes the condition false.
What do you think happens?
A loop uses while True. Before reading the body, what must you locate to decide whether the loop has an intentional termination path?
Reveal answer
A specific event or break statement
The condition True never becomes false. According to the source, such a loop may intentionally run until a specific event occurs, such as user input or a break statement.
Key Takeaways
- 1An infinite loop never terminates because its condition never becomes false.
- 2A terminating loop has a finite starting value, a condition that checks it, and an update that moves it toward a stopping point.
- 3A loop with no iteration variable or with a condition that is always true can run indefinitely.
- 4while True is intentional only when a clear event or exit mechanism, such as break, ends the loop.
- 5To diagnose a runaway loop, inspect the condition, the values it depends on, and every control-flow path that repeats.
At a glance
- An infinite loop occurs when its condition never becomes false.
- Finite iteration requires a changing value that the condition checks and that can reach a stopping point.
- The constant True in a while condition cannot become false, so another intentional exit is required.
- break can provide that intentional exit when a specific event occurs.
- Diagnosing a runaway loop means tracing the condition, its dependent values, and every repeated control-flow path.