Concepts / Breaking Out of Loops: Using break and continue

Breaking Out of Loops: Using break and continue

A loop's body does not control termination by itself. Termination depends on the loop condition and on whether the values used by that condition move toward a stopping point.

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

Why Loops Keep Running

An infinite loop never terminates because its condition never becomes false. The important question is therefore not simply whether a loop contains repeated work. The important question is whether the condition controlling the loop can eventually become false.

Key point

A loop's body does not control termination by itself. Termination depends on the loop condition and on whether the values used by that condition move toward a stopping point.

Following Control Flow

Loop conditionLoop bodycontinuenext iterationLoop exitbreakloop exittruefalsecontinue reachedbreak reachedcheck againleave loopiteration completes
Where does execution go when a loop reaches its condition, break, or continue?

Use the diagram as a tracing model. A normal iteration returns to the loop condition after the loop body completes. A break provides an exit from a loop before the condition becomes false. The source pack identifies break as a possible event that intentionally ends a while True loop. The source pack does not provide a detailed definition of continue, so the key diagnostic lesson here is to trace every control-flow path and ask whether it can reach a terminating condition or an intentional exit.

Finite Progression

Finite startingvaluechecked by conditionVariable changestoward stopping pointStopping pointcondition becomes falseAlways-trueconditionnever falseRepeated loop bodyno terminating progressionAnother iterationiteration runsmoves toward endtruerepeatscondition checked again
How does an iteration variable move toward a stopping point in one loop, while remaining unable to reach one in another?

A terminating loop has three connected features: a finite starting value, a condition that checks that value, and code inside the loop that moves the value toward a stopping point. If the condition eventually becomes false, the loop terminates. The body can perform many different tasks, but those tasks matter for termination only when they affect the values used by the condition.

Loop structureWhat the condition depends onTermination risk
Finite iterationA finite variable that changes toward a stopping pointThe condition can become false
No iteration variableNo changing value is directing the loop toward an endThe loop can run indefinitely
Constant True conditionA condition that is always trueThe condition never becomes false

The relationship between the condition and the values it checks determines whether the loop can terminate.

Worked example

Tracing a finite iteration

A loop begins with a finite iteration variable. The loop condition checks that variable, and the loop body changes it toward a stopping point. What should you inspect to decide whether the loop terminates?

  1. 1
    Find the starting value

    Confirm that the loop has a finite starting value rather than no iteration variable at all.

  2. 2
    Find the condition

    Identify the condition that checks the iteration variable. This condition is the direct authority on whether another iteration begins.

  3. 3
    Trace the update

    Follow the change made to the variable inside the loop. The change must move the value toward the stopping point used by the condition.

  4. 4
    Check the endpoint

    Decide whether the variable can reach a value that makes the condition false. If it can, the loop has a terminating path.

Result

A finite iteration is structurally safe when its condition checks a changing value that can reach a stopping point.

The while True Pattern

The constant True in a while condition creates an infinite loop because the condition itself never becomes false. Each time the condition is checked, it produces the same true result, so the loop keeps returning to its body unless another intentional exit is provided.

Trueconstant conditionLoop bodyrepeated workNext checkTrue againbreakspecific exit eventalways trueno exit reachedcheck againbreak reached
Why does a while True structure return to the loop body instead of following a condition-changing path to termination?

Runaway Loop Diagnosis

Repeated iterationssame loop continuesInspect conditionwhat must become false?Inspect variabledoes it change?Restore terminationcondition or intentionalexitInspect control pathcan it reach an exit?begin tracingfind dependent valuestrace each iterationexit is missing or unreachable
How can repeated iterations reveal the condition, variable, or control-flow path that prevents termination?
  1. 1
    Start with the loop condition. Write down exactly what must become false for normal termination.
  2. 2
    Identify every variable or value used by that condition.
  3. 3
    Trace whether the loop body changes those values.
  4. 4
    Check whether each control-flow path reaches a condition-changing update or an intentional break.
  5. 5
    If the condition is while True or there is no iteration variable, locate the specific event that is supposed to end the loop.
  6. 6
    If no terminating path is clear, treat the loop as a likely bug and stop examining the repeated path until its exit logic is understood.
  • Common mistake

    Assuming that work inside the loop automatically makes the loop terminate.

    What it looks like

    A loop body performs repeated tasks, but the value checked by the condition never changes.

    Why it fails

    The loop body does not control termination by itself. The condition must eventually become false, or an intentional exit must be reached.

    Do this instead

    Trace the values used by the condition and verify that the body moves them toward a stopping point.

  • Common mistake

    Treating the presence of a variable as proof that the loop is finite.

    What it looks like

    A loop has a variable, but that variable is not changed toward the stopping point.

    Why it fails

    A terminating loop needs a finite starting value, a condition that checks it, and code that moves it toward the end.

    Do this instead

    Check the complete relationship among the starting value, condition, and update.

  • Common mistake

    Reading while True without asking how it exits.

    What it looks like

    A loop uses the constant True, but no specific event or break statement provides an exit.

    Why it fails

    True never becomes false, so the condition cannot provide normal termination.

    Do this instead

    Locate the intended event or break statement. If no exit is clear, investigate the loop as a likely bug.

  • Common mistake

    Assuming every infinite loop is accidental.

    What it looks like

    A programmer uses while True intentionally until user input or another specific event occurs.

    Why it fails

    Infinite loops can be deliberate when a specific event controls their exit.

    Do this instead

    Distinguish an intentional event-controlled loop from a loop whose exit path is missing or unclear.

Practice the Trace

MEDIUM

Consider a loop whose condition checks a finite iteration variable. The variable begins with a finite value, but one path through the loop does not move it toward the stopping point. Identify the two questions you should ask before deciding whether the loop terminates.

Hints
  • First identify the value checked by the condition.
  • Then trace whether every repeated path changes that value toward a point that makes the condition false.

What do you think happens?

A loop uses while True. Before reading the body, what must you locate to decide whether the loop has an intentional termination path?

  • A finite starting value

  • A specific event or break statement

  • A second loop condition

  • A different iteration variable

Reveal answer
Answer:

A specific event or break statement

The condition True never becomes false. According to the source, such a loop may intentionally run until a specific event occurs, such as user input or a break statement.

Key Takeaways

  1. 1
    An infinite loop never terminates because its condition never becomes false.
  2. 2
    A terminating loop has a finite starting value, a condition that checks it, and an update that moves it toward a stopping point.
  3. 3
    A loop with no iteration variable or with a condition that is always true can run indefinitely.
  4. 4
    while True is intentional only when a clear event or exit mechanism, such as break, ends the loop.
  5. 5
    To diagnose a runaway loop, inspect the condition, the values it depends on, and every control-flow path that repeats.

At a glance

  • An infinite loop occurs when its condition never becomes false.
  • Finite iteration requires a changing value that the condition checks and that can reach a stopping point.
  • The constant True in a while condition cannot become false, so another intentional exit is required.
  • break can provide that intentional exit when a specific event occurs.
  • Diagnosing a runaway loop means tracing the condition, its dependent values, and every repeated control-flow path.

Related concepts

More concepts starting with B