INITIALIZE LOOP VARIABLE
WHILE CONDITION IS TRUE
DO SOMETHING AND 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. We have already seen a number of counting loops, like this one that counts from 1 to 5:
Although we could count by ones using i = i + 1, most programmers use the ++ or increment operator to increase a value by one. And they use -- to decrement a value (decrease it by one). When we introduced these operators back in SectionΒ 3.12, we discussed the difference between i++ and ++i But it is worth quickly reviewing and expanding on what we learned there.
When used on their own with integer variables, they do the same thing - they increase the variable by one. However, if you mix them with other logic, the order of ++ and the variable determines when the variable in increased:
i++; // increase i by 1. It would happen after any other code in the statement
++i; // increase i by 1. It would happen before any other code in the statement
As we will learn later, for more complex data, the preincrement version can be more efficient. So it is generally a good habit to use ++i in C++.
The loop at the top of the page repeats 5 times by counting from 1 to 5. Most programmers would write a loop that repeats 5 times as starting from 0 and counting up to but not including 5, like this:
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:
That program will behave exactly the same if we count from 0-4 or if we count from 1-5. We just need to make sure that we count the right number of things. If we start from 0 and count up to and including 5, we would repeat 6 times. If we start counting from 1 and count up to but not including 5, we would only repeat 4 times.
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. Think carefully about whether to use < or <= in a particular loop so that it repeats the right number of times.
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 assume that it always means it is now OK to name variables using single letters. Variables should always have meaningful names. However, for programmers who are used to thinking of i as always being βloop counterβ, it is a meaningful name. It means βloop counterβ.
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). Code inside the loop that works with the variable, like the print statement, makes more sense if it is clear that the thing we are counting is actually the months:
The program below should print out the even numbers between 20 and 40, inclusive (including 20 and 40), but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.
int main() {
---
main(int) { #paired
---
int n = 20;
---
int n = 0; #distractor
---
while (n <= 40) {
---
while (n < 40) { #paired
---
cout << n << endl;
---
n = n + 2;
---
n++; #distractor
---
n = n * 2; #distractor
---
}
---
}
The program below should count down from 100 to 0 by 10 but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.
int main() {
---
int n = 100;
---
int n = 10; #distractor
---
while (n >= 0) {
---
while (n < 0) { #distractor
---
while (n > 0) { #distractor
---
cout << n << endl;
---
n -= 10;
---
n += 10; #distractor
---
}
---
}