|
|
The for loop allows automatic initialization and incrementation of a counter variable. The general form is
for(initialization; condition; increment) { statement block }
If the statement block is only one statement, the braces are not necessary.
Although the for allows a number of variations, generally the initialization sets a loop control variable to its starting value. The condition is usually a relational statement that checks the loop control variable against a termination value, and increment increments (or decrements) it. If the condition is false to begin with, the body of the for loop will not execute even once.
The following statement will print the message “hello” ten times:
for(t=0; t<10; t++) cout << "hello\n";
|
|