For is simply an extension of while. It provides an
even shorter and more compact way of writing loops. The syntax looks
For does the following steps:
for ( initializer_statement ; expression ; incrementor_expression )
This means that the example in the while section can be written like this:
- Executes the the initializer_statement. The initializer statement
is executed only once and is most commonly used to initialize the loop
- 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.
for(int e=1; e<5; e=e+1)