Concepts / Using continue to Skip to the Next Iteration

Using continue to Skip to the Next Iteration

The break statement exits a loop immediately, skipping any remaining code in the loop body.

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

  • The program prints done and starts another iteration

  • The loop exits immediately and skips the print inside the loop

  • The loop ignores done and continues forever

Reveal answer
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.

Loop startsRead inputInput is donePrint inputDone!breaknonext iterationyesexit loop
When break executes, which statements and future iterations are skipped?

The Input Trace

Worked example

Reading Until done

Trace the described program when the user enters hello, world, and then done.

  1. 1
    First 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.

  2. 2
    Second 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.

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

  4. 4
    After the loop

    Control continues with the statement after the loop. The program prints Done! once.

Result

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.

while TrueRead inputStop conditionLoop workAfter loopeach iterationnot metnext iterationmet: break
How does repeated execution continue, and where does break provide the exit path?

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.

Begin iterationObtain inputStop conditionLater loop workNext iterationAfter loopnot metrepeatmet: break
How does control flow change when the loop obtains information before checking whether it should stop?

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 phrasingAffirmative phrasing
Keep going while the input is not doneStop when the input is done
The reader must interpret a negationThe stopping event is stated directly
The loop condition describes continuationThe 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 like

    The program reaches break and is expected to execute the rest of the current loop body.

    Why it fails

    Break exits the loop immediately, so the remaining loop-body statements do not run.

    Do this instead

    Trace the path from break to the statement after the loop.

  • Common mistake

    Assuming the loop will naturally stop after break executes.

    What it looks like

    The stop input is entered, but the learner expects another iteration afterward.

    Why it fails

    Break skips every later iteration as well as the remainder of the current one.

    Do this instead

    Mark the loop as finished as soon as break executes.

  • Common mistake

    Creating a while True loop without a realistic break condition.

    Why it fails

    The loop has no effective exit path and can become an accidental infinite loop.

    Do this instead

    Check 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 fails

    A condition cannot inspect the current input until the program has obtained that input.

    Do this instead

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

    The reader must mentally interpret the negation instead of seeing the stopping event directly.

    Do this instead

    When appropriate, express the event that means stop and use break.

Control-Flow Practice

MEDIUM

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

  1. 1
    Break exits a loop immediately and skips the remaining statements in its current loop body.
  2. 2
    The while True pattern creates an intentional infinite loop whose actual exit is supplied by break.
  3. 3
    An exit condition can be checked inside the loop body, including after earlier work in the iteration.
  4. 4
    Affirmative phrasing states when to stop directly, while a negative loop condition states when to keep going.
  5. 5
    Every 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.

Related concepts

More concepts starting with U