The for-statement provides a means of executing repeatedly a given statement, the "controlled statement", for different values of a chosen variable, which may (or may not) occur within the controlled statement. A typical form of for-statement is
Other forms are exemplified by
which is self-explanatory, and
In the latter example, the clause "i + 1 WHILE x < y" counts as a single for-element and could be used as one element in a list of for-elements (the "for-list"). As each for-element is exhausted, the next element in the list is taken. The syntax is
Forstatement
::=
FOR
Wordreference
:= Forlist
DO
Statement
Forlist
::=
Forelement
Forelement
, Forlist
Forelement
::=
Expression
Expression
WHILE
Condition
Expression
STEP
Expression
UNTIL
Expression
The controlled variable is a word reference, i.e. either an anonymous reference or a declared word reference.
Let the element be denoted by
e1
STEPe1
UNTILe3
In contrast to Algol 60, the expressions are evaluated only once. Let their values be denoted by v1, v2 and v3 respectively. Then
v1 is assigned to the control variable,
v1 is compared with v3. If (v1 - v3) * v2 > 0, then the for-element is exhausted, otherwise
the controlled statement is executed,
the value of v1 is set from the controlled variable, then incremented by v2 and the cycle is repeated from (a).
Let the element be denoted by
e1
WHILECondition
Then the sequence of operation is
e1
is evaluated and assigned to the control variable,
the condition is tested. If false, the for-statement is exhausted, otherwise
the controlled statement is executed and the cycle repeated from (i).
Unlike those in , the expression e1
and
those occurring in the condition are evaluated repeatedly.