The loop performs initialization just once. Typically, programs use this expression to set a variable to a starting value and then use the variable to count loop cycles.
Listing 5.2. num_test.cpp
// num_test.cpp -- use numeric test in for loop
#include
int main()
{
using namespace std;
cout << "Enter the starting countdown value: ";
int limit;
cin >> limit;
int i;
for (i = limit; i; i--) // quits when i is 0
cout << "i = " << i << "\n";
cout << "Done now that i = " << i << "\n";
return 0;
}
Here is the output from the program in Listing 5.2:
Enter the starting countdown value: 4
i = 4
i = 3
i = 2
i = 1
Done now that i = 0
Note that the loop terminates when i reaches 0.
How do relational expressions, such as i < 5, fit into this framework of terminating a loop with a 0 value? Before the bool type was introduced, relational expressions evaluated to 1 if true and 0 if false. Thus, the value of the expression 3 < 5 was 1, and the value of 5 < 5 was 0. Now that C++ has added the bool type, however, relational expressions evaluate to the bool literals true and false instead of 1 and 0. This change doesn’t lead to incompatibilities, however, because a C++ program converts true and false to 1 and 0 where integer values are expected, and it converts 0 to false and nonzero to true where bool values are expected.
The for loop is an
Enter the starting countdown value: 0
Done now that i = 0
This look-before-you-loop attitude can help keep a program out of trouble.
The for loop body consists of a single statement, but you’ll soon learn how to stretch that rule. Figure 5.1 summarizes the for loop design.
Figure 5.1. The design of for loops.
A for statement looks something like a function call because it uses a name followed by paired parentheses. However, for’s status as a C++ keyword prevents the compiler from thinking for is a function. It also prevents you from naming a function for.
Tip
Common C++ style is to place a space between for and the following parenthesis and to omit space between a function name and the following parenthesis:
for (i = 6; i < 10; i++)
smart_function(i);
Other control statements, such as if and while, are treated similarly to for. This serves to visually reinforce the distinction between a control statement and a function call. Also common practice is to indent the body of a for statement to make it stand out visually.
Expressions and Statements
A for control section uses three expressions. Within its self-imposed limits of syntax, C++ is a very expressive language. Any value or any valid combination of values and operators constitute an expression. For example, 10 is an expression with the value 10 (no surprise), and 28 * 20 is an expression with the value 560. In C++ every expression has a value. Often the value is obvious. For example, the following expression is formed from two values and the addition operator, and it has the value 49:
22 + 27
Sometimes the value is less obvious. For example, the following is an expression because it’s formed from two values and the assignment operator:
x = 20