The keyword defines a counter loop. The value of the index variable is increased, decreased or manipulated in such a way that after a certain number of execution loops a termination criterion is reached.
for ([ index_variable_declaration ] initial_value; condition; manipulator_expression) { statement_block }; |
The for keyword introduces the loop.
index_variable_declaration is optional. When used, the variable will only be visible within the loop.
initial_value is used to initialize the index (or counter) variable of the loop.
conditionis a Boolean expression. It is checked at the beginning of each new loop iteration. If it evaluates to false, the loop terminates.
manipulator_expression is stepping the index variable after each new loop iteration.
statement_block contains one or more statements. The block will be executed in a loop until condition is true.
Example 1:
var integer j;
for (j:=1; j<=10; j:= j+2) { log ("index variable: ", j) }
The loop variable j is declared in the first line. In the second line, a loop is defined. The initial value of the index variable (j) is 1. The index variable is increased by 2 every time the loop was executed. The loop execution terminates when the index value will have a value greater than 10. The statement block contains a log instruction, which will be executed 5 times resulting in five lines in the log.
Example 2:
for (var integer jj:=1; jj<=10; jj:= jj+2) { log ("index variable: ", jj) }
The only difference to the first example is that the index variable (jj) now is declared within the loop. It means that it will not be visible outside the loop.
BNF definition of for