up previous next
loop command
For I := N_1 To N_2 Do C EndFor
For I := N_1 To N_2 Step D Do C EndFor
where I is the loop variable, N_1, N_2, and D are integer expressions,
and C is a sequence of commands.

In the first form, the loop variable
I
is assigned the values
N_1, N_1+1, ...,N_2
in succession. After each assignment,
the command sequence
C
is executed. If
N_2 < N_1
, then
the command sequence
C
is not executed.
The second form is almost the same, except that
I
is assigned the
values
N_1, N_1+D, N_1+2D
, and so on, until the limit
N_2
is
passed. If
N_2  N_1
has opposite sign to
D
, then the command
sequence
C
is not executed.
/**/ for N := 1 to 5 do print 2^N, " "; endfor;
2 4 8 16 32
/**/ for n := 1 to 20 step 3 do print n, " "; endfor;
1 4 7 10 13 16 19
/**/ for N := 10 to 1 step 2 do print N, " "; endfor;
10 8 6 4 2
/**/ for N := 5 to 3 do print N, " "; endfor;  no output

Loops can be nested.
/**/ define MySort(ref L)
/**/ for i := 1 to len(L)1 do
/**/ MaxPos := i;
/**/ for j := i+1 to len(L) do
/**/ if L[j] < L[MaxPos] then MaxPos := j; endif;
/**/ endfor;
/**/ if MaxPos <> i then
/**/ swap(ref L[i], ref L[MaxPos]);
/**/ endif;
/**/ endfor;
/**/ enddefine;
/**/ M := [5,3,1,4,2];
/**/ MySort(ref M);
/**/ M;
[1, 2, 3, 4, 5]

(Note that
ref L
is used so that the function can change the value
of the variable referenced by
L
. See
ref
.)