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 a dummy variable, N_1, N_2, and D are integer expressions,
and C is a sequence of commands.
|
In the first form, the variable
I
is assigned the values
N_1, N_1+1, ...,N_2
in succession. After each assignment,
he command sequence
C
is executed. The second form is 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
, then the command sequence
C
is not executed.
NOTE: Large values for
N_1, N_2
, or
D
are not permitted;
typically they should lie in the range about
-10^9 to
+10^9.
/**/ 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
/**/ M := I;
/**/ For J := I+1 To len(L) Do
/**/ If L[J] < L[M] Then M := J; EndIf;
/**/ EndFor;
/**/ If M <> I Then
/**/ C := L[M];
/**/ L[M] := L[I];
/**/ L[I] := C;
/**/ 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
.)