for
Syntax:
for( initialization; test-condition; increment ) {
statement-list;
}
The for construct is a general looping mechanism consisting of 4
parts:
- the initialization, which consists of 0 or more comma-delimited
variable initialization statements
- the test-condition, which is evaluated to determine if the
execution of the for loop will continue
- the increment, which consists of 0 or more comma-delimited
statements that increment variables
- and the statement-list, which consists of 0 or more statements
that will be executed each time the loop is executed.
For example:
for( int i = 0; i < 10; i++ ) {
cout << "i is " << i << endl;
}
int j, k;
for( j = 0, k = 10;
j < k;
j++, k-- ) {
cout << "j is " << j << " and k is " << k << endl;
}
for( ; ; ) {
// loop forever!
}
Related topics: