Skip to main content

Section 8.3 Counting Loops

Every loop has the same conceptual structure:
INITIALIZE LOOP VARIABLE
WHILE CONDITION IS TRUE
    DO SOMETHING
    UPDATE LOOP VARIABLE
But there are some different ways to apply that basic pattern that are worth learning. The first in the counting loop. A counting loop does what the title advertises: it counts. It starts at a number, and then counts up or down to another number. Like this loop that counts from 1 to 5:
Listing 8.3.1.
Note that to get 5 repetitions when starting from 1, we need to count up to and including 5. For reasons that will become clear when we introduce arrays and strings, computer scientists often number things starting from 0. So often times, we count from 0. Most programmers would write a loop that repeats 5 times as starting from 0 and counting up to but not including 5, like this:
Listing 8.3.2.
Note that the program repeats the body 5 times, but the output is now 0, 1, 2, 3, 4. If we care about the numbers being printed, that would be a problem. But often, we arenโ€™t doing much with the loop control variable other than tracking the progress of the loop. Say I want to print out "Hello" 5 times. It doesnโ€™t matter what my counter is doing, as long as it causes the loop to repeat 5 times:
Listing 8.3.3.
That sample could initialize line to 1 and count up to 5. It could initialize line to 5 and count down (--line) while line was greater than 0. It could even start from 0, count by 10s (line += 10) and stop at 50. (That would probably be confusing however... โ€œwait, why is this loop counting by 10s? Is that important?โ€)
Because the loop control variable has no meaning other than โ€œthe variable that is controlling the loopโ€, programmers have developed the convention of naming an otherwise meaningless loop counter i. If we need a second loop counter somewhere that i is already in use, we use j, then k, etc...
You should get used to seeing i used in this way, but donโ€™t interpret that as meaning it is now OK to name variables using single letters. Variables should still have meaningful names. It is just that for programmers who are used to thinking of i as always being โ€œloop counterโ€, it is a meaningful name.
Also, if the loop counter has a meaning beyond โ€œloop counterโ€, you should consider giving it a real name, not just i. Say you are counting off the months of the year 1-12. When written like this, even if you only see the tail end of the loop on your screen, it is clear that we are printing out the month:
Listing 8.3.4.
int month = 1;
while (month <= 12) {
    //lots of other code
    //...
    //lots of other code
    cout << month << endl;
    month++;
}
If we used i it would be easier to lose sight of the fact that i really means โ€œmonth numberโ€ in this loop.

Warning 8.3.1.

Off by one errors are one of the most common types of errors in programs. An off by one error occurs when a loop goes one step too far, or stops one step too early, or counts from 1-5 when it should count from 0-4. Pay close attention to whether you should use < or <= in loops.

Checkpoint 8.3.1.

The program below should print out the even numbers between 20 and 40, inclusive, but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.

Checkpoint 8.3.2.

The program below should count down from 100 to 0 in decrements of 10 but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.
You have attempted of activities on this page.