FORcontrollist
instructionlist
FOR is a flexible tool for iteration.
The controllist
input controls how the iteration occurs.
It must be a list containing either three or four members.
In order, they are:
A word, which is used as the name of a "control variable". The control variable is local to the procedure.
A word or list that is evaluated to determine a number, the "starting value" of the variable.
A word or list that is evaluated to determine a number, the "limit value" of the variable.
An optional word or list that is evaluated to determine the "step size". If the fourth member is missing, the step size is 1 or -1 depending on whether the limit value is greater than or less than the starting value, respectively.
The instructionlist
input dictates what FOR should do on each iteration.
The effect of FOR is to run instructionlist
repeatedly, assigning a new value to the "control variable" on each iteration.
First the "starting value" is assigned to the control variable.
Then the control value is compared to the "limit value".
FOR is complete when the sign of (current - limit) is the same as the sign of the "step size".
If no "step size" is provided, the instructionlist
is always run at least once.
An explicit step size can lead to a zero-trip FOR, (for example, FOR [I 1 0 1] [
).
Otherwise, ...
]instructionlist
is run, then the control variable is incremented by the step size and FOR returns to the comparison step.
In many cases, a FOR instruction can be rewritten to use REPEAT and REPCOUNT, instead.
FOR [I 2 7 1.5] [PRINT :I]
2
3.5
5
6.5