6.8. The for Loop StatementThe syntactic form of a for statement is for (init-statement condition; expression) statement The init-statement must be a declaration statement, an expression statement, or a null statement. Each of these statements is terminated by a semicolon, so the syntactic form can also be thought of as for (initializer; condition; expression) statement although technically speaking, the semicolon after the initializer is part of the statement that begins the for header. In general, the init-statement is used to initialize or assign a starting value that is modified over the course of the loop. The condition serves as the loop control. As long as condition evaluates as true, statement is executed. If the first evaluation of condition evaluates to false, statement is not executed. The expression usually is used to modify the variable(s) initialized in init-statement and tested in condition. It is evaluated after each iteration of the loop. If condition evaluates to false on the first iteration, expression is never executed. As usual, statement can be either a single or a compound statement.
Using a for LoopGiven the following for loop, which prints the contents of a vector, for (vector<string>::size_type ind = 0; ind != svec.size(); ++ind) { cout << svec[ind]; // print current element // if not the last element, print a space to separate from the next one if (ind + 1 != svec.size()) cout << " "; } the order of evaluation is as follows:
These four steps represent the first iteration of the for loop. Step 2 is now repeated, followed by steps 3 and 4, until the condition evaluates to falsethat is, when ind is equal to svec.size().
6.8.1. Omitting Parts of the for HeaderA for header can omit any (or all) of init-statement, condition, or expression. The init-statement is omitted if an initialization is unnecessary or occurs elsewhere. For example, if we rewrote the program to print the contents of a vector using iterators instead of subscripts, we might, for readability reasons, move the initialization outside the loop: vector<string>::iterator iter = svec.begin(); for( /* null */ ; iter != svec.end(); ++iter) { cout << *iter; // print current element // if not the last element, print a space to separate from the next one if (iter+1 != svec.end()) cout << " "; } Note that the semicolon is necessary to indicate the absence of the init-statement more precisely, the semicolon represents a null init-statement. If the condition is omitted, then it is equivalent to having written true as the condition: for (int i = 0; /* no condition */ ; ++i) It is as if the program were written as for (int i = 0; true ; ++i) It is essential that the body of the loop contain a break or return statement. Otherwise the loop will execute until it exhausts the system resources. Similarly, if the expression is omitted, then the loop must exit through a break or return or the loop body must arrange to change the value tested in the condition: for (int i = 0; i != 10; /* no expression */ ) { // body must change i or the loop won't terminate } If the body doesn't change the value of i, then i remains 0 and the test will always succeed. 6.8.2. Multiple Definitions in the for HeaderMultiple objects may be defined in the init-statement; however, only one statement may appear, so all the objects must be of the same general type: const int size = 42; int val = 0, ia[size]; // declare 3 variables local to the for loop: // ival is an int, pi a pointer to int, and ri a reference to int for (int ival = 0, *pi = ia, &ri = val; ival != size; ++ival, ++pi, ++ri) // ...
|