Concepts / Loop Conditions: Writing Expressions That Control Repetition

Loop Conditions: Writing Expressions That Control Repetition

Changing something inside the loop is not enough by itself. The changed value must be connected to the condition in a way that can eventually make the condition false.

  • Programming

The Question at Every Repetition

A loop repeats because its condition permits another iteration. The most useful question when reading a loop is not only what the loop body does, but also how the condition could eventually become false. If the condition never becomes false, the loop is infinite and never terminates.

Tracing a Finite Loop

Worked example

Following an iteration variable toward a stopping point

Imagine a loop with an iteration variable that starts at a finite value, is checked by the condition, and changes inside the loop so that it moves toward the stopping point.

  1. 1
    Starting value

    The iteration variable begins with a finite value.

  2. 2
    Condition check

    The loop condition checks the current value before another repetition proceeds.

  3. 3
    Loop body

    The loop body changes the iteration variable.

  4. 4
    Movement toward stopping

    The change moves the variable toward the point at which the condition will be false.

  5. 5
    Termination

    Once the condition becomes false, the loop stops instead of entering another iteration.

Result

The loop terminates because the condition depends on a finite variable that changes toward the stopping point.

finite valuestarting valueconditionchecks the variableloop bodychanges the variablestopping pointcondition becomes falseinitial checkcondition truechanged valuecondition false
How does the iteration variable move toward termination during repeated condition checks?

The important relationship is between the condition and the variable it examines. A terminating loop has a finite starting value, a condition that checks that value, and code in the loop body that moves the value toward the stopping point.

Condition, Body, and Termination

A loop condition controls whether another repetition occurs. The loop body may change data, but the body does not independently decide that the loop has finished. Termination occurs only when the condition becomes false. Therefore, when tracing a loop, inspect the condition and then identify which values used by that condition can change inside the body.

conditionevaluated before repetitionloop bodyone iterationterminationcondition falsetruefalsecheck again
What happens to the condition after each iteration, and how does the loop reach the point where it stops?
Key point

Changing something inside the loop is not enough by itself. The changed value must be connected to the condition in a way that can eventually make the condition false.

Two Structural Causes of Infinite Loops

Infinite loops arise from two main structural patterns. One pattern has no iteration variable that moves toward a stopping point. The other uses a condition that is always true, such as while True:. In both cases, the condition never becomes false, so control keeps returning to the loop body.

conditiondepends on finite variableiteration variablechanges toward stoppingTruealways truecontrol valuedoes not reach falsecheckschangesremains trueno false result
How does a terminating loop differ from a loop whose controlling information does not move toward termination?
Trueconstant conditionloop bodyrepeated workcondition checkTrue againtruerepeatalways true
Why does control return to the loop body forever when the while condition is always True?
Edge case

An infinite loop is not automatically a mistake. Experienced programmers may intentionally use while True: when they want repetition to continue until a specific event occurs or a break statement is reached. The important diagnostic question is whether the exit mechanism is clear.

Finding a Runaway Cycle

To diagnose a runaway loop, trace the control cycle: condition check, loop body, and return to the condition. Then ask which value the condition depends on and whether that value changes toward a false result. If there is no iteration variable, or if the condition is always true, the cycle has no visible route to termination.

conditionnever falseloop bodyrepeatsconditioncan become falseiteration variablemoves toward stoppingterminationcondition falsetruerepeatschecksreaches stopping point
Where does control keep cycling, and what change makes a termination path possible?
  • Common mistake

    Assuming that the loop body controls termination by itself.

    What it looks like

    The body performs work repeatedly, but the condition never changes to false.

    Why it fails

    Only the condition determines whether another iteration occurs.

    Do this instead

    Trace the values used by the condition and check whether the body moves them toward a false result.

  • Common mistake

    Seeing a changing value and assuming the loop must terminate.

    What it looks like

    A value changes inside the body, but the condition does not depend on that value.

    Why it fails

    A change that is unrelated to the condition does not create a stopping path.

    Do this instead

    Connect the changing iteration variable to the condition and verify that it moves toward the stopping point.

  • Common mistake

    Treating while True: as if it contained a built-in stopping point.

    What it looks like

    The condition is the constant True, so every condition check permits another iteration.

    Why it fails

    The condition itself never becomes false.

    Do this instead

    Identify the specific event or break statement intended to provide the exit, or redesign the loop around a finite changing condition.

  • Common mistake

    Ignoring a loop with no iteration variable.

    What it looks like

    The loop repeats without a finite value that changes and is checked.

    Why it fails

    There is no visible progress toward a false condition.

    Do this instead

    Ask what value will eventually make the condition false before allowing the loop to continue.

Practice the Trace

MEDIUM

Consider two loop designs. In the first, the condition checks a finite iteration variable that changes inside the loop toward a stopping point. In the second, the condition is while True: and no exit event or break statement is identified. For each design, state whether it has a visible termination path and explain why.

Hints
  • Look at the value used by the condition, not just at the work performed by the body.
  • For the first design, describe how the variable moves toward a false condition.
  • For the second design, ask what could change the constant True condition or provide the intended exit.

What do you think happens?

A loop condition is always True, and no specific event or break statement is provided. What should you predict?

  • The loop has a visible finite stopping point

  • The loop never terminates

  • The loop terminates because the body runs repeatedly

Reveal answer
Answer:

The loop never terminates.

An infinite loop never terminates because its condition never becomes false. A constant True condition continues to permit another iteration unless a separate intentional exit is provided.

Key Takeaways

  1. 1
    A loop repeats while its condition permits another iteration and stops when that condition becomes false.
  2. 2
    A terminating loop has a finite iteration variable, checks that variable in its condition, and changes it toward a stopping point.
  3. 3
    A loop can run indefinitely when it lacks a changing iteration variable or uses a condition that is always true, such as while True:.
  4. 4
    The loop body does not independently control termination; termination depends on the condition.
  5. 5
    When diagnosing a runaway loop, trace the condition-body-condition cycle and identify the value or event that should provide an exit.

At a glance

  • A loop terminates only when its condition becomes false.
  • Finite iteration variables provide a route toward termination when they are checked and changed appropriately.
  • No changing iteration variable and a constant True condition are two structural causes of infinite loops.
  • Intentional infinite loops require a clear exit event or break statement.
  • Diagnosing a runaway loop means tracing what the condition depends on and how that value can reach a stopping point.

Related concepts

More concepts starting with L