for (initialization; comparison; continuation) { instructions... }
Performs the initialization, then
evaluates the comparison and
repeats
all instructions between the winged brackets as long as the comparison
is true resp. non-zero. The continuation statement will be executed
after the instructions and before the next repetition. This repetition of instructions
is called a loop.
Initialization and continuation can be any expression or
function call. A for loop is often used to increment a counter for a fixed
number of repetitions.
Remarks:
- Loops can be prematurely terminated by break, and prematurely repeated
by continue.
- The winged brackets can be omitted when the loop contains only
one instruction.
Example:
double x = 3;
for(int i=0; i<5; i++) // repeat 5 times
x *= x; // calculate the 5th power of x
See also:
if, while, goto, break, continue, comparisons
► latest
version online