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:
- Executes the the initializer_statement. The initializer statement
is executed only once and is most commonly used to initialize the loop
variable.
- Evaluates expression
- If the result was false it exits the loop and continues with the
program after the loop.
- Executes statement.
- Executes the increment_expression.
- 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);