Concepts / Breaking Out of Loops: Using break and continue

Breaking Out of Loops: Using break and continue

An infinite loop never terminates because its condition never becomes false.

  • Programming

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.

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

truefalsecontinue reachedbreak reachedcheck againleave loopiteration completesLoop conditionLoop bodycontinuenext iterationLoop exitbreakloop exit
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

iteration runsmoves toward endtruerepeatscondition checked againFinite startingvaluechecked by conditionVariable changestoward stopping pointStopping pointcondition becomes falseAlways-trueconditionnever falseRepeated loop bodyno terminating progressionAnother iteration
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

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?

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

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

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.

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.

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.

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

Runaway Loop Diagnosis

begin tracingfind dependent valuestrace each iterationexit is missing or unreachableRepeated iterationssame loop continuesInspect conditionwhat must become false?Inspect variabledoes it change?Restore terminationcondition or intentionalexitInspect control pathcan it reach an exit?
How can repeated iterations reveal the condition, variable, or control-flow path that prevents termination?
  1. Start with the loop condition. Write down exactly what must become false for normal termination.
  2. Identify every variable or value used by that condition.
  3. Trace whether the loop body changes those values.
  4. Check whether each control-flow path reaches a condition-changing update or an intentional break.
  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. 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.
  • Assuming that work inside the loop automatically makes the loop terminate.

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

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

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

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

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

  • Reading while True without asking how it exits.

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

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

  • Assuming every infinite loop is accidental.

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

    Fix: 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. An infinite loop never terminates because its condition never becomes false.
  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. A loop with no iteration variable or with a condition that is always true can run indefinitely.
  4. while True is intentional only when a clear event or exit mechanism, such as break, ends the loop.
  5. To diagnose a runaway loop, inspect the condition, the values it depends on, and every control-flow path that repeats.

Key Takeaways

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