Previous section To contents Next section

3.2.2 for

For is simply an extension of while. It provides an even shorter and more compact way of writing loops. The syntax looks like this:
for ( initializer_statement ; expression ; incrementor_expression )
    statement ;
For does the following steps:
  1. Executes the the initializer_statement. The initializer statement is executed only once and is most commonly used to initialize the loop variable.
  2. Evaluates expression
  3. If the result was false it exits the loop and continues with the program after the loop.
  4. Executes statement.
  5. Executes the increment_expression.
  6. Starts over from 2.
This means that the example in the while section can be written like this:
for(int e=1; e<5; e=e+1)
    show_record(e);

Previous section To contents Next section