Concepts / Skipping Iterations with continue

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.

  • Programming
Interactive lab

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

  1. Evaluate the condition. If it is False, skip the body and continue after the loop.
  2. If it is True, run the body from the top, then go back and evaluate the condition again.
  3. continue skips the rest of the body and goes straight back to the condition check.
  4. 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').
  5. 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.

Educational simulation

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.

Conditiontrue or falseLoop bodyState updateLoop exittruefalsenormal completioncheck again
How does control move from the condition check through the loop body and back to the next condition check?
Key point

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.

ConditiontrueSkip testcurrent valuecontinueNext condition checkRemaining bodytrueskip condition metskip condition not metjumpfinish body
When the skip condition is met, which statements are skipped and where does control go next?

Tracing Selected Iterations

Worked example

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.

  1. 1
    Start

    The loop condition is true, so the first value enters the loop body.

  2. 2
    Value 1

    The value does not meet the skip condition, so the remaining processing statements run. The loop then advances to the next value.

  3. 3
    Value 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.

  4. 4
    Values 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.

  5. 5
    After value 5

    The state update produces a value beyond the intended range. The condition becomes false, so the loop exits.

Result

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.

1processedLoop exit2skipped3processed4skipped5processedadvancecontinueadvancecontinuecondition false
What happens to the loop state and executed statements as values are processed or skipped?

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.

Condition trueiteration beginsCondition variableupdatedCondition checkCondition falseSame conditioninfinite loopstate update occurscontinue or body completionfalsetrueno state update
How does the loop reach a false condition, and what can prevent termination when continue is used?
  • Common mistake

    Treating continue as if it ended the entire loop

    What it looks like

    A skipped iteration is followed by another condition check.

    Why it fails

    continue skips the remainder of the current body but does not make the loop condition false.

    Do this instead

    Trace the next condition check separately from loop termination.

  • Common mistake

    Failing to update the condition variable on a skipped path

    What it looks like

    The loop reaches continue while the value used by the condition remains unchanged.

    Why it fails

    The condition may remain true on every check, producing an infinite loop.

    Do this instead

    Ensure the condition variable changes before control reaches continue.

  • Common mistake

    Assuming statements after continue still run

    What it looks like

    Processing statements placed later in the same loop body are expected to run for a skipped value.

    Why it fails

    Those statements belong to the portion of the current iteration that continue bypasses.

    Do this instead

    Place 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.

SituationSuitable control decisionReason
Current value should not receive the remaining processingUse continueThe current iteration is skipped while the loop can continue
The loop condition has become falseAllow the loop to exitA while loop exits when its condition is false
The number of repetitions is known in advanceConsider a for loopThe 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

MEDIUM

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

  1. 1
    A while loop checks its condition before every iteration and exits when that condition is false.
  2. 2
    continue skips the remaining statements in the current iteration and returns control to the next condition check.
  3. 3
    The condition variable must change on every relevant path, including paths that use continue.
  4. 4
    Tracing the condition, state update, and executed statements makes skipped iterations easier to understand.
  5. 5
    Use 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.

Related concepts

More concepts starting with S