Syntax:
for (expression1; expression2; expression3)
operator
Expression1 describes the cycle initialization. Expression2 is checking the condition of the cycle completion. If it is True, then:
the "for" operator of the cycle body will be executed;
expression3 will be executed.
Everything will be repeated until expression2 becomes False.
If it is False, then the cycle will be finished and control will be passed to the next operator.
Expression3 is calculated after each iteration.
The "for" operator is equivalent to the following operator sequence:
expression1;
while (expression2)
{
operator
expression3;
Example:
for(x = 1; x <= 7; x++)
printf("%d\n", pow(x, 2));
In any of the three expressions, or in all three expressions of the operator, "for" may be absent, but the semicolons (;) separating them cannot be omitted.
If expression2 is omitted, then it will be considered True. The "for" operator (;;) is the endless cycle equivalent to the While(1) operator.
|