Skip to main content

Section 8.2 Loop Control Variables

The condition for a loop almost always has a variable:
while (count < 10)
while (money > 0)
Try running the two programs. Which one produces output that is most intuitive?
That variable is called the loop control variable because it controls how many times the loop runs. In order for the loop to eventually stop, the loop control variable must be changed inside the body of the loop so that the condition will eventually become false.
Here are two loops that both attempt to count by 1. In both, the loop control variable is i - it is the variable that is being tested to determine if the loop should continue.
Listing 8.2.1.
Listing 8.2.2.
The first version displays the values 1 to 5, which is what you would likely expect looking at the initial value of i and the loop’s condition. Notice that it increments the loop control variable (i) at the end of the loop. The second version increments the loop control variable at the start of the loop. Because of this, the 1 we start with is changed to 2 before it is printed. And when i is 5, it makes the condition true, but then is changed to 6 (which would have stopped the loop) before we use it.

Insight 8.2.1.

Once we start the loop body, changing the loop control variable can’t stop the loop until we reach the end of the loop body and redo the condition check.
The location we change the loop control variable splits the code inside a loop into part that works with the current value (that was tested in the condition) and part that works with the next value.
while (i < 5) {
  // i still has the old value we tested in the condition
  i = i + 1;   // update to next value
  // i now has the new value
}
Usually, we want to use the value we just tested in the condition. Then, after we are done using that value, we update it to the next value for the next iteration of the loop. The logic is something like this:
INITIALIZE LOOP VARIABLE
AS LONG AS LOOP VARIABLE STILL MEETS CONDITION
    DO SOMETHING WITH CURRENT VALUE OF LOOP VARIABLE
    UPDATE LOOP VARIABLE TO NEXT VALUE
After updating the loop variable, we immediately return back to the test and get to decide β€œShould we actually run the loop with this value? Or should we stop?”.
Although there are times when you might change the loop control at the start of the loop, or even in the middle of the loop, the logic for those situations tends to be more complex. Any code that is after the change to the loop control variable is working with the β€œnext” value. We will not yet have even tested that value to see if we want to continue working with it.

Insight 8.2.2.

Modify the loop control variable at the end of the loop unless you have a specific reason to do otherwise. All of your work should be done with the β€œcurrent” value, not the β€œnext” one.
It is possible to have a variable that is part of our condition that is not the loop control variable. Consider this sample:
double goal = 10000;
double current = 1000;
while (current < goal) {
    cout << current << endl;
    current = current * 1.1;
}
Here, goal is part of the condition, but it is not the loop control variable. The loop control variable is current because it is the variable that is changing inside the loop body and will eventually make the condition false.

Checkpoint 8.2.1.

Checkpoint 8.2.2.

You have attempted of activities on this page.