Skipping Iterations with continue
The loop does not decide its next iteration by counting repetitions. It decides by evaluating its condition again. This is why a change to the condition variable inside the body is necessary for reliable termination.
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 Repeating Cycle
A while loop repeatedly executes its body as long as its condition is true. Before every iteration, Python evaluates the condition. If the condition is false, the loop exits immediately. If the condition is true, Python enters the body and later returns to the condition check.
The loop does not decide its next iteration by counting repetitions. It decides by evaluating its condition again. This is why a change to the condition variable inside the body is necessary for reliable termination.
Where continue Redirects Control
continue is used when the current iteration should stop before the remaining statements in the loop body run. Control moves directly to the next while-loop condition check. If the condition is still true, a new iteration begins; if it is false, the loop exits. In this way, continue skips work for the current iteration without ending the entire loop.
Tracing Selected Iterations
Process only values that pass a check
Imagine a while loop examining values from 1 through 5. The loop should skip even values and process odd values. The loop must still advance after every iteration.
- 1Start
The loop condition is true, so the first value enters the loop body.
- 2Value 1
The value does not meet the skip condition, so the remaining processing statements run. The loop then advances to the next value.
- 3Value 2
The value meets the skip condition. continue skips the remaining processing statements and sends control to the next condition check. The loop still advances to the next value.
- 4Values 3, 4, and 5
The same decision repeats. Values 3 and 5 are processed, value 4 is skipped, and the loop advances after each iteration.
- 5After value 5
The state update produces a value beyond the intended range. The condition becomes false, so the loop exits.
The loop processes 1, 3, and 5 while skipping 2 and 4. continue changes what happens during selected iterations, but termination still comes from the condition becoming false.
A useful tracing habit is to record three things for every iteration: the condition value before entry, the value of the condition variable, and which statements run. When continue is reached, mark the remaining body as skipped, then trace the next condition check. Do not erase the iteration from your trace: it occurred, but only part of its body executed.
Termination and Infinite Loops
A while loop terminates when its condition becomes false. Because continue moves control to the next condition check, the loop variable or other state needed for termination must already have been changed before continue is reached. If every path that reaches continue leaves the condition variable unchanged, the same condition can remain true forever.
Common mistake Treating continue as if it ended the entire loop
What it looks likeA skipped iteration is followed by another condition check.
Why it failscontinue skips the remainder of the current body but does not make the loop condition false.
Do this insteadTrace the next condition check separately from loop termination.
Common mistake Failing to update the condition variable on a skipped path
What it looks likeThe loop reaches continue while the value used by the condition remains unchanged.
Why it failsThe condition may remain true on every check, producing an infinite loop.
Do this insteadEnsure the condition variable changes before control reaches continue.
Common mistake Assuming statements after continue still run
What it looks likeProcessing statements placed later in the same loop body are expected to run for a skipped value.
Why it failsThose statements belong to the portion of the current iteration that continue bypasses.
Do this insteadPlace any required state update before continue or use a control structure that keeps the update on every path.
Practical Loop Design
Use a while loop when the number of repetitions depends on a condition that changes during execution. A practical filtering task can follow this pattern: check whether another value remains, examine the current value, skip values that should not be processed, perform the main work for the remaining values, update the loop state, and return to the condition check.
| Situation | Suitable control decision | Reason |
|---|---|---|
| Current value should not receive the remaining processing | Use continue | The current iteration is skipped while the loop can continue |
| The loop condition has become false | Allow the loop to exit | A while loop exits when its condition is false |
| The number of repetitions is known in advance | Consider a for loop | The source notes that a for loop is usually clearer and safer for processing each item in a known collection |
Choosing between skipping an iteration, completing a while loop, and using a for loop
Trace a while loop that examines values from 1 through 6, skips multiples of 3, and processes every other value. For each iteration, record the current value, whether continue is reached, whether the processing step runs, and whether the loop condition is still true afterward.
Hints
- The skipped values are 3 and 6.
- A skipped iteration still needs to advance the state used by the loop condition.
- The loop exits only after the state moves beyond the final value.
Key Takeaways
- 1A while loop checks its condition before every iteration and exits when that condition is false.
- 2continue skips the remaining statements in the current iteration and returns control to the next condition check.
- 3The condition variable must change on every relevant path, including paths that use continue.
- 4Tracing the condition, state update, and executed statements makes skipped iterations easier to understand.
- 5Use a while loop when repetition depends on a changing condition; use a for loop when processing a known sequence is clearer.
At a glance
- A while loop repeats while its condition is true.
- continue skips the rest of the current iteration and starts the next condition check.
- Skipping work does not automatically update the loop state or terminate the loop.
- Every path through the loop must support progress toward a false condition.
- Trace each iteration by recording the condition, state, and statements that execute.