Concepts / Counting Up with a Loop

Counting Up with a Loop

A countdown loop repeatedly decreases a control variable until a condition is no longer true, printing or using the variable at each step.

  • Programming

From Start to Stop

A countdown loop begins with a control variable and repeatedly reduces its value. During each repetition, the program can print or use the current value. The loop continues while its condition is true and stops when that condition is no longer true.

The essential pattern has four parts: initialize the variable, check it with a while condition, use its current value inside the loop, and decrement it before the next repetition.

What do you think happens?

Suppose n starts at 3, the loop continues while n is greater than 0, and the loop prints n before subtracting 1. Which values are printed?

  • 3, 2, 1
  • 2, 1, 0
  • 3, 2, 1, 0
  • 3, 4, 5
Reveal answer

Answer: 3, 2, 1

The current value is used before each decrement. The loop prints 3, then changes n to 2; prints 2, then changes n to 1; and prints 1, then changes n to 0. At that point, the condition n > 0 is false.

Tracing Each Iteration

n = 3 while n > 0: print(n) n = n - 1

decrementdecrementdecrementIteration 1n = 3; print 3Iteration 2n = 2; print 2Iteration 3n = 1; print 1Iteration 4n = 0; condition false
What value does the control variable have at each iteration, and what does the loop print before the next decrement?
Iteration stateCurrent nAction
First check passes3Print 3, then decrement
Second check passes2Print 2, then decrement
Third check passes1Print 1, then decrement
Next check fails0Stop the loop

A trace of the control variable for the generated countdown example.

The Control Variable

The control variable is the value that determines whether another repetition may occur. In a countdown loop, the variable moves downward because the loop body contains the update n = n - 1. This update is called decrementation. It is the step that drives the countdown forward and prevents the loop from repeatedly testing the same value.

n = n - 1n3n2
How does n = n - 1 change the control variable from one iteration to the next?
truethenloop backfalseCheck conditionn > 0Use nprint or use current valueDecrement nn = n - 1Stopcondition is false
What happens first in a while countdown loop: checking the condition, using the current value, or decrementing the variable?

Finding the Stopping Point

A countdown loop terminates when the control variable reaches a value that no longer satisfies the while condition. In the example with n > 0, the loop body runs for positive values. After the final decrement, n becomes 0, so the next condition check is false and the body is not run again.

n = n - 1n = 1n > 0 is truen = 0n > 0 is false
After which decrement does the condition become false, and why does the loop stop at that point?
next iterationnext iteration3printed first2printed second1printed third
Which sequence of values appears in the output as the loop runs from its starting value toward termination?

Locating the Final Check

Trace a loop that starts with n = 4, continues while n > 1, prints n, and then performs n = n - 1.

Start: n is 4, so the condition n > 1 is true. The loop prints 4 and decrements n to 3.

Continue: n is 3, so the condition is still true. The loop prints 3 and decrements n to 2.

Final body execution: n is 2, so the condition is still true. The loop prints 2 and decrements n to 1.

Terminate: n is now 1. The condition n > 1 is false, so the loop stops without printing 1.

The output is 4, 3, 2. The loop terminates when the control variable reaches 1 because 1 does not satisfy n > 1.

Mistakes to Avoid

  • Forgetting to decrement the control variable

    The condition can remain true because n is not changed inside the loop.

    Fix: Place n = n - 1 inside the loop after the value is used.

  • Using a condition that does not match the intended stopping point

    This condition stops when n reaches 1, so 1 is not printed.

    Fix: Choose the while condition according to which values should be included before termination.

  • Putting the decrement outside the loop

    The update does not occur during each repetition, so it cannot drive the countdown forward.

    Fix: Indent the decrement so it runs as part of every loop iteration.

  • Decrementing by the wrong amount

    The control variable changes by two rather than by the one-step decrement used in the basic countdown pattern.

    Fix: Use n = n - 1 when each successive countdown value should be one less than the previous value.

When checking a countdown loop, trace three things for every iteration: the value before the body runs, the value printed or used, and the value after n = n - 1. Then evaluate the condition once more to locate the exact stopping point.

Practice the Trace

MEDIUM

Trace this countdown loop without running it. Write the output values in order and identify the value of n that causes the loop to stop. n = 5 while n > 2: print(n) n = n - 1

Hints
  • The current value is printed before it is decremented.
  • After each printed value, subtract 1 from n.
  • The loop stops when n is no longer greater than 2.

Practice Check

Determine the output and stopping value for the practice loop.

First iteration: n is 5, so the condition is true. Print 5, then decrement n to 4.

Second iteration: n is 4, so the condition is true. Print 4, then decrement n to 3.

Third iteration: n is 3, so the condition is true. Print 3, then decrement n to 2.

Condition check: n is 2, and 2 is not greater than 2. The condition is false, so the loop stops.

The output is 5, 4, 3. The stopping value is n = 2.

Key Takeaways

  1. A countdown loop initializes a control variable and checks it with a while condition.
  2. The loop uses the current value before applying the decrement.
  3. The update n = n - 1 moves the control variable toward the stopping condition.
  4. The loop terminates when the condition becomes false.
  5. Forgetting or misplacing the decrement can prevent the countdown from progressing correctly.

Key Takeaways

  • A countdown loop repeatedly decreases a control variable while a condition remains true.
  • The basic sequence is initialize, check, use the current value, decrement, and check again.
  • The statement n = n - 1 is the critical update that advances the countdown.
  • Termination occurs after a decrement makes the while condition false.
  • Tracing the variable before and after every iteration makes the output and stopping point predictable.