1.6.5 for

The for statement allows for a controlled loop.

Syntax:

for( expression1 ; expression2 ; expression3 ) statement…

expression1 is evaluated before the first iteration. After each iteration, expression3 is evaluated. Both expression1 and expression3 may be ommited. Ifexpression2 is ommited, it is assumed to be 1. statement is executed repeatedly until the value of expression2 is 0. The test on expression2 occurs before each execution of statement.

Examples:

for(loop=0;loop<1000;loop++)
  printf("%i\n",loop);

Prints numbers 0 through 999.

for(x=3, y=5; x<100+y; x++, y--)
 {
  printf("%i\n",x);
  some_function();
 }

Prints numbers 3 through 53. some_function is called 51 times.

Tagged: