C++ adds a feature to C loops that requires some artful adjustments to the for loop syntax. This was the original syntax:
for (
In particular, the control section of a for structure consisted of three expressions, as defined earlier in this chapter, separated by semicolons. C++ loops allow you do to things like the following, however:
for (int i = 0; i < 5; i++)
That is, you can declare a variable in the initialization area of a for loop. This can be convenient, but it doesn’t fit the original syntax because a declaration is not an expression. This once outlaw behavior was originally accommodated by defining a new kind of expression, the
for (
At first glance, this looks odd because there is just one semicolon instead of two. But that’s okay because
There’s a practical aspect to declaring a variable in
for (int i = 0; i < 5; i++)
cout << "C++ knows loops.\n";
cout << i << endl; // oops! i no longer defined
Another thing you should know is that some older C++ implementations follow an earlier rule and treat the preceding loop as if i were declared
Back to the for Loop
Let’s be a bit more ambitious with loops. Listing 5.4 uses a loop to calculate and store the first 16 factorials. Factorials, which are handy for computing odds, are calculated the following way. Zero factorial, written as 0!, is defined to be 1. Then, 1! is 1 * 0!, or 1. Next, 2! is 2 * 1!, or 2. Then, 3! is 3 * 2!, or 6, and so on, with the factorial of each integer being the product of that integer with the preceding factorial. (One of the late pianist-comedian Victor Borge’s best-known monologues featured phonetic punctuation, in which the exclamation mark is pronounced something like phffft pptz, with a moist accent. However, in this case, “!” is pronounced “factorial.”) The program uses one loop to calculate the values of successive factorials, storing them in an array. Then it uses a second loop to display the results. Also the program introduces the use of external declarations for values.
Listing 5.4. formore.cpp
// formore.cpp -- more looping with for
#include
const int ArSize = 16; // example of external declaration
int main()
{
long long factorials[ArSize];
factorials[1] = factorials[0] = 1LL;
for (int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i-1];
for (int i = 0; i < ArSize; i++)
std::cout << i << "! = " << factorials[i] << std::endl;
return 0;
}
Here is the output from the program in Listing 5.4:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
Factorials get big fast!
Note
This listing uses the long long type. If your system doesn’t have that type available, you can use double. However, the integer format gives a nicer visual representation of how the numbers grow larger.
Program Notes
The program in Listing 5.4 creates an array to hold the factorial values. Element 0 is 0!, element 1 is 1!, and so on. Because the first two factorials equal 1, the program sets the first two elements of the factorials array to 1.0. (Remember, the first element of an array has an index value of 0.) After that, the program uses a loop to set each factorial to the product of the index with the previous factorial. The loop illustrates that you can use the loop counter as a variable in the body of the loop.