Using continue to Skip to the Next Iteration
The break statement exits a loop immediately, skipping any remaining code in the loop body.
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…
The Stop Decision
The supplied material for this article focuses on break, not on the behavior of continue. Break exits a loop immediately when a specific condition is met. It also skips every remaining statement in the current loop body. The central question is this: when the program discovers that it should stop, how does control flow leave the loop?
Tracing an Immediate Exit
What do you think happens?
A loop reads hello, then world, then done. What happens when the input is done?
Reveal answer
The loop exits immediately and skips the print inside the loop.
The stop condition is true when the input equals done, so break executes. The remaining loop-body statement is skipped, and control moves to the statement after the loop.
The Input Trace
Reading Until done
Trace the described program when the user enters hello, world, and then done.
- 1First iteration
The loop starts and waits for input. The user enters hello. The condition asking whether the input equals done is false, so break does not execute. The program prints hello and returns to the top of the loop.
- 2Second iteration
The program waits for input again. The user enters world. The condition is false again, so the program prints world and returns to the top.
- 3Third iteration
The user enters done. This time the condition is true, so break executes. The loop exits immediately, and the print of the current input is skipped.
- 4After the loop
Control continues with the statement after the loop. The program prints Done! once.
hello and world are printed from inside the loop. done is not printed by that loop-body statement. The loop ends, and Done! is printed afterward.
The important boundary is the moment break executes. It does not merely skip one output or postpone a decision. It exits the loop immediately, so both the rest of the current loop body and every later iteration are skipped.
Intentional Infinite Loops
The while True pattern creates an intentional infinite loop. Its repeated execution is not meant to continue forever in a finished program; break supplies the actual path out when a specific condition is met. This pattern is useful when the program must first perform work, such as obtaining input, before it can know whether it should stop.
Exit Conditions Inside the Body
A traditional loop condition is checked at the top of a loop. The break pattern allows the program to check its exit condition anywhere in the loop body instead. This matters when the program must obtain input or perform an earlier step before the stopping information exists.
In the input example, the condition belongs after the input operation because the program cannot compare the current input with done until that input has been received. When the condition is false, the remaining work runs. When it is true, break prevents that remaining work from running.
Affirmative Stop Conditions
| Negative phrasing | Affirmative phrasing |
|---|---|
| Keep going while the input is not done | Stop when the input is done |
| The reader must interpret a negation | The stopping event is stated directly |
| The loop condition describes continuation | The break condition describes exit |
Both approaches can express the same stopping behavior, but the break-based version states the stop condition directly.
The while True with break pattern lets you say when to stop rather than when to keep going. The affirmative form can be clearer because the reader sees the stopping event directly. The source material notes that this is especially valuable when the stop condition is complex or when there are multiple possible exit points.
Common Control-Flow Mistakes
Common mistake Treating break as if it only skips the current statement.
What it looks likeThe program reaches break and is expected to execute the rest of the current loop body.
Why it failsBreak exits the loop immediately, so the remaining loop-body statements do not run.
Do this insteadTrace the path from break to the statement after the loop.
Common mistake Assuming the loop will naturally stop after break executes.
What it looks likeThe stop input is entered, but the learner expects another iteration afterward.
Why it failsBreak skips every later iteration as well as the remainder of the current one.
Do this insteadMark the loop as finished as soon as break executes.
Common mistake Creating a while True loop without a realistic break condition.
Why it failsThe loop has no effective exit path and can become an accidental infinite loop.
Do this insteadCheck that realistic program input or state can make the break condition true.
Common mistake Placing the exit test before the information needed for that test exists.
Why it failsA condition cannot inspect the current input until the program has obtained that input.
Do this insteadPlace the break check where the relevant information becomes available.
Common mistake Using a negatively phrased condition when an affirmative stop condition would be clearer.
Why it failsThe reader must mentally interpret the negation instead of seeing the stopping event directly.
Do this insteadWhen appropriate, express the event that means stop and use break.
Control-Flow Practice
Trace a loop that reads three inputs in this order: alpha, done, beta. Identify which inputs are printed inside the loop, when break executes, whether beta is processed, and what kind of statement can run after the loop.
Hints
- The stop condition is checked after each input is received.
- Break skips the rest of the current loop body and every later iteration.
- Compare the input that triggers the stop with the inputs that do not.
The trace is alpha first, then done. Alpha does not satisfy the stop condition, so the ordinary loop work occurs. Done satisfies the condition, so break exits immediately. Beta is not processed because the loop has already ended. Control then moves to the statement after the loop.
Key Takeaways
- 1Break exits a loop immediately and skips the remaining statements in its current loop body.
- 2The while True pattern creates an intentional infinite loop whose actual exit is supplied by break.
- 3An exit condition can be checked inside the loop body, including after earlier work in the iteration.
- 4Affirmative phrasing states when to stop directly, while a negative loop condition states when to keep going.
- 5Every break-based loop should have a realistic path to its exit.
At a glance
- Break leaves a loop immediately and skips all remaining loop-body statements.
- The while True pattern is useful when the program discovers its stopping condition inside the loop.
- Checking an exit condition inside the body allows the condition to be placed where the relevant information becomes available.
- Affirmative stop conditions can be clearer than negatively phrased keep-going conditions.
- A break statement must be reachable under realistic conditions, or the loop may run indefinitely.