Real1 | Real2 | → |
The FOR operation is used to define a loop structure within a program. It is combined with the NEXT or STEP operation to define the boundaries of the loop.
The normal way FOR is used is one of the following:
≪ ... Real1 Real2 FOR Symbol3 ... operations ... NEXT ... ≫
or
≪ ... Real1 Real2 FOR Symbol3 ... operations ... Real4 STEP ... ≫
When FOR is executed, it pops two values of the stack which should be real values. Real1 is the starting value for the loop counter and Real2 is the ending value for the loop counter. Immediately following the FOR operation is a symbol name. A local variable which is valid only in the body of the loop is then created with that name which contains the loop counter. This allows you to access the loop counter in the body of the loop.
When NEXT is executed, the loop counter is incremented by one. If the loop counter is less than or equal to the ending value, then execution jumps back to the operation following FOR and executes the body of the loop again. If the value is greater than the ending value after incrementing the loop counter, then execution continues after the NEXT operation, exiting the loop.
The STEP operation can be used in place of NEXT to specify an increment for the loop counter which is not one. When STEP is executed the top of the stack is popped. It expects to find a real value there. It adds that real value to the loop counter and then tests to see if the loop counter has reached the end. For a negative increment, the loop stops when the counter is less than the ending value. For a positive increment, the loop stops when the counter is greater than the ending value. If not yet at the end, it loops back to the operation following FOR. Otherwise, it exits the loop and execution continues after the STEP operation.
An error will be raised if this operation is used outside of program execution context.