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.

test-expression determines whether the loop body gets executed. Typically, this expression is a relational expression—that is, one that compares two values. Our example compares the value of i to 5, checking whether i is less than 5. If the comparison is true, the program executes the loop body. Actually, C++ doesn’t limit test-expression to true/false comparisons. You can use any expression, and C++ will type cast it to type bool. Thus, an expression with a value of 0 is converted to the bool value false, and the loop terminates. If the expression evaluates to nonzero, it is type cast to the bool value true, and the loop continues. Listing 5.2 demonstrates this by using the expression i as the test condition. (In the update section, i-- is similar to i++ except that it decreases the value of i by 1 each time it’s used.)

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 entry-condition loop. This means the test expression is evaluated before each loop cycle. The loop never executes the loop body when the test expression is false. For example, suppose you rerun the program in Listing 5.2 but give 0 as a starting value. Because the test condition fails the very first time it’s evaluated, the loop body never gets executed:

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.

update-expression is evaluated at the end of the loop, after the body has been executed. Typically, it’s used to increase or decrease the value of the variable keeping track of the number of loop cycles. However, it can be any valid C++ expression, as can the other control expressions. This makes the for loop capable of much more than simply counting from 0 to 5, the way the first loop example does. You’ll see some examples of this later.

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

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги