Concepts / Variable Assignment and Modification

Variable Assignment and Modification

A while loop repeats code as long as a condition is true, checking the condition before each iteration.

  • Programming

Why Repetition Needs Change

A loop is useful when the same kind of work must be repeated many times. Printing the numbers 5, 4, 3, 2, 1 and then saying Blastoff! could be written as separate instructions, but writing hundreds or thousands of separate instructions would be impractical. A while loop lets a program repeat a block of code while a condition remains true. For that repetition to end, something inside the loop must change.

The central question when reading a while loop is: what changes after each pass, and will that change eventually make the condition false?

The Countdown Trace

Consider the source example that counts down from 5 to 1. The variable n begins at 5. Before every iteration, the condition n > 0 is checked. When the condition is true, the loop body prints the current value and decreases n. The loop then returns to the condition check.

truethencheck againeventually n = 0falseCondition truen > 0Loop bodyprint nn decreasestoward 0Condition falsen = 0Blastoff!next statement
What happens each time the condition is checked, the loop body runs, and control returns to the condition?
Condition checkValue of nCondition resultWhat happens
15trueThe body runs, prints 5, and n becomes 4
24trueThe body runs, prints 4, and n becomes 3
33trueThe body runs, prints 3, and n becomes 2
42trueThe body runs, prints 2, and n becomes 1
51trueThe body runs, prints 1, and n becomes 0
6
0
false
The loop stops and the next statement prints Blastoff!

A manual trace of the source countdown

An iteration is one complete pass through the loop body. In this countdown, the body runs five times, so the loop has five iterations. The later check with n equal to 0 is not another iteration because the body does not run during that check.

The Iteration Variable

The iteration variable is the variable that changes inside the loop body and eventually causes the condition to become false. In the countdown, n is the iteration variable. Its initial value allows the first condition check to succeed, and each decrease moves the loop closer to stopping.

decreasedecreasedecreasedecreasedecreasen = 5n > 0 is truen = 4n > 0 is truen = 3n > 0 is truen = 2n > 0 is truen = 1n > 0 is truen = 0n > 0 is false
How does the iteration variable's value change after each loop iteration, and how does that change affect the next condition check?

The Stopping Check

A while loop checks its condition before every execution of the body. If the condition is true, the body runs. After the body finishes, control returns to the beginning and the condition is evaluated again. If the condition is false, the loop exits immediately and execution continues with the statement after the loop.

Finding the termination point

In the source countdown, when does the loop stop?

Start: n begins at 5, so the condition n > 0 is true and the body can run.

Repeat: Each completed iteration prints the current value and decreases n by one.

Final body execution: When n is 1, the condition is still true. The body prints 1 and decreases n to 0.

Exit: The next check evaluates n > 0 with n equal to 0. The result is false, so the body does not run again.

The loop has five iterations and stops when the condition is checked with n equal to 0. The next statement then prints Blastoff!

The condition check that ends a loop is not an iteration unless the loop body executes. In the countdown, the body executes for 5, 4, 3, 2, and 1; the check at 0 only confirms that the loop must stop.

When Repetition Never Ends

trueno decreasecheck againn > 0truePrint nbody runsnvalue unchanged
What happens when the iteration variable never changes in a way that can make the condition false?

An infinite loop occurs when the condition never becomes false. In the source example, a loop that checks n > 0 and prints n but never decreases n will keep running because n remains greater than 0. The condition is checked repeatedly, but the state needed for termination never changes.

  • Assuming that a while loop stops after a certain number of body executions automatically.

    A while loop continues as long as its condition is true; it does not stop merely because the body has already run several times.

    Fix: Identify the iteration variable and verify that the body changes it toward a value that makes the condition false.

  • Changing the iteration variable in the wrong direction.

    The condition can remain true forever when the variable does not move toward the stopping value.

    Fix: Trace the variable after each iteration and check whether the next condition check is closer to false.

  • Counting the final false condition check as an iteration.

    An iteration is a complete execution of the loop body. The body does not execute when the condition is false.

    Fix: Count only the passes in which the body runs.

Before running a while loop, write down the condition, the iteration variable, and the change that occurs in the body. Then ask whether repeated changes can make the condition false. This simple check helps prevent infinite loops.

Manual Tracing Practice

Use a written trace whenever a loop produces unexpected output. For each condition check, record the iteration variable, whether the condition is true or false, and what the body changes. This reveals the exact point where execution differs from your expectation.

EASY

Create a trace for a countdown that begins with n equal to 3, uses the condition n > 0, prints n during the body, and decreases n after printing. Record every condition check, identify the number of iterations, and state the value that causes the loop to stop.

Hints
  • Start with n equal to 3 before the first condition check.
  • Record the values for which the body runs before recording the final false check.
  • The loop stops when n is no longer greater than 0.

What do you think happens?

In the source countdown, what happens when the condition is checked with n equal to 0?

  • The body runs and prints 0
  • The loop stops and the next statement runs
  • The value of n increases
  • The loop starts again at 5
Reveal answer

Answer: The loop stops and the next statement runs.

With n equal to 0, the condition n > 0 is false. The body is skipped, and execution continues after the loop.

Reliable Loop Tracing

  1. A while loop checks its condition before every possible iteration.
  2. An iteration is one complete execution of the loop body.
  3. The iteration variable changes inside the body and must move toward making the condition false.
  4. A loop terminates when its condition evaluates to false.
  5. Tracing the variable and condition step by step helps reveal extra iterations and infinite loops.

Key Takeaways

  • A while loop repeats its body while its condition is true and checks that condition before each iteration.
  • The iteration variable must change in a direction that can make the condition false.
  • The number of iterations equals the number of times the loop body executes.
  • A loop becomes infinite when its condition never becomes false, often because the iteration variable does not change appropriately.
  • A manual trace of the variable and condition can expose unexpected or extra iterations.