Concepts / Loop Control Flow

Loop Control Flow

Statements after continue in the same loop block do not run for the iteration that reached continue. They can run during later iterations if those iterations do not reach continue.

  • 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…

A Selective Skip

A loop does not always need to process every iteration in the same way. Sometimes one value should be skipped while the loop continues handling later values. Python provides continue for this situation. When Python reaches continue, it stops executing the current iteration and returns to the top of the loop to begin the next iteration.

The Continue Jump

A continue statement divides the current loop iteration into two possible paths. If execution reaches continue, Python immediately stops the current iteration. Every statement after continue and before the end of the loop block is skipped for that iteration. Execution then returns to the top of the loop, where the next iteration begins. If the condition that leads to continue is not met, execution proceeds through the remaining statements in the loop block normally.

Loop iterationSkip conditioncontinueNext iterationRemaining statementscurrent iterationchecktruejump to topfalsefinish iteration
When Python reaches continue, which part of the current iteration is skipped and where does execution go next?
Key point

Statements after continue in the same loop block do not run for the iteration that reached continue. They can run during later iterations if those iterations do not reach continue.

Tracing an Iteration

Worked example

Tracing the input sequence

For the input sequence hi, hello, x, and stop, determine what happens when the loop skips inputs shorter than three characters.

  1. 1
    hi

    The input has fewer than three characters, so continue is reached. Processing after continue is skipped, and the loop returns to the top for the next input.

  2. 2
    hello

    The input meets the minimum length requirement, so continue is not reached. The processing statement runs before the loop begins its next iteration.

  3. 3
    x

    The input has fewer than three characters, so continue is reached. Processing after continue is skipped, and the loop moves to the next iteration.

  4. 4
    stop

    This input is part of the stated sequence. The continue rule concerns the minimum length, so this input meets that requirement and the processing statement runs.

Result

The short inputs hi and x are skipped by continue. The processing statement runs for hello and stop.

hicontinuehelloprocessxcontinuestopprocessnext iterationnext iterationnext iteration
For each input, does execution skip the remaining body or reach the processing statement?

What do you think happens?

Which inputs reach the processing statement: hi, hello, x, or stop?

  • hi and x

  • hello and stop

  • all four inputs

  • only stop

Reveal answer
Answer:

hello and stop

hi and x are shorter than three characters, so continue skips the processing statement for those iterations. hello and stop meet the minimum length requirement, so their processing statements run.

Filtering Without Exiting

A common use of continue is filtering. The loop can inspect each input, skip inputs that do not meet a minimum requirement, and continue processing later inputs. In the source example, strings shorter than three characters are not processed. The loop does not end after encountering a short string; it asks for or handles the next iteration instead.

InputLength below 3continueskip processingNext inputProcess inputinspecttruenext iterationfalsenext iteration
How can a loop skip selected values while continuing to process later values?
Best practice

Place continue at the point where the loop can clearly decide that the current item should be skipped. Then keep the processing statements after it. This makes the skip path and the process path easier to trace.

Continue Compared with Break

Continue and break both alter loop execution, but they do not have the same scope. Continue stops only the current iteration and returns to the top of the loop. Break exits the loop entirely. Use continue when later iterations should still be considered. Use break when the loop itself should end.

continuecurrent iterationNext iterationbreakentire loopAfter loopskips remainderexits loop
How does continue affect one iteration while break exits the entire loop?
StatementImmediate effectWhat happens afterward
continueStops the current iterationExecution returns to the top of the loop for the next iteration
breakExits the loop entirelyThe loop does not continue with later iterations

The essential difference between continue and break

Mistakes in Control Tracing

  • Common mistake

    Treating continue as if it ends the loop

    What it looks like

    A short input reaches continue, and the learner assumes no later inputs will be handled.

    Why it fails

    Continue skips only the rest of the current iteration. Execution returns to the top of the loop for the next iteration.

    Do this instead

    Trace the next iteration after every continue before deciding whether the loop has ended.

  • Common mistake

    Counting statements after continue as executed for that iteration

    What it looks like

    The learner includes the final processing statement in the output for a short input.

    Why it fails

    Statements between continue and the end of the loop block are skipped during that iteration.

    Do this instead

    Mark the current iteration as skipped from the continue point onward.

  • Common mistake

    Using the effect of break when explaining continue

    What it looks like

    The learner says that continue exits the entire loop.

    Why it fails

    Break exits the loop entirely, while continue returns to the top of the loop.

    Do this instead

    Ask whether later iterations should still run. If they should, the described behavior is continue, not break.

Practice the Trace

MEDIUM

Trace the source input sequence hi, hello, x, and stop. For each input, write whether continue is reached, whether the processing statement runs, and which input is handled next.

Hints
  • Compare each input with the minimum length of three characters.
  • If continue is reached, skip the processing statement for that iteration.
  • After a continue, trace the next input rather than ending the loop.
hiomittedhelloprintedxomittedstopprintednext iterationnext iterationnext iteration
Which values are omitted because continue is reached first, and which values reach the output statement?

Key Takeaways

  1. 1
    Continue stops the current loop iteration immediately.
  2. 2
    Statements after continue in the same loop block are skipped for that iteration.
  3. 3
    Execution returns to the top of the loop and begins the next iteration.
  4. 4
    Continue is useful for skipping selected inputs while still processing later inputs.
  5. 5
    Break exits the entire loop, so it has a different effect from continue.

At a glance

  • Continue skips the rest of only the current iteration.
  • After continue, execution returns to the top of the loop for the next iteration.
  • Statements after continue do not execute during the iteration that reaches it.
  • Continue can filter out selected inputs without ending the loop.
  • Break exits the entire loop, unlike continue.

Related concepts

More concepts starting with L