FOR
PURPOSE:
To initiate a FOR/NEXT loop.
The loop variable is set and the termination value and
the loop variable increment on placed on the run time stack.
STATEMENT SYNTAX:
{stno} FOR loopvar = expra TO exprb {STEP exprc}
where:
loopvar= a SIMPLE NUMERIC VARIABLE whose value will control
the termination of the FOR/NEXT loop. The memory address of this
loop variable is placed on the run time stack so it is available to
the NEXT command.
expra= a numeric expression to which the loop variable is initialized.
exprb= a numeric expression whose value when compared to the current
value of the loop variable, during execution of the NEXT command,
determines if FOR/NEXT loop execution should terminate.
The value of this expression is placed on the run time stack so it is
available to the NEXT command.
exprc= a numeric expression that determines the amount
that the loop variable is incremented during each execution of the NEXT
command. If exprc is missing the value 1 is used. This value is
placed on the run time stack so it is available to the NEXT command.
EXAMPLE:
0100 FOR X = 0 TO 1
0110 FOR Y=10 TO 1 STEP -1
0120 PRINT Y+X,
0130 NEXT Y
0140 PRINT ""
0150 NEXT X
>RUN
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2
>
NOTES:
The FOR/NEXT loop will be executed at least once
since the checking for loop termination is done by the NEXT statement.
If the increment value is 0 the loop will never terminate.
If the increment value is positive the FOR/NEXT loop will terminate when
the current value of the loop variable is greater than the termination
value(exprb). If the increment value is negative the FOR/NEXT loop
will terminate when the current value of the loop variable is less than the
termination value(exprb).
If during the NEXT command the FOR/NEXT loop does not terminate:
- program flow is transferred to the statement following the FOR statement.
If during the NEXT command the FOR/NEXT loop does terminate:
- the information stored by the FOR onto the run time stack is removed.
- program flow is continues with the statement following the NEXT statement.
If during the NEXT command the information on the run time stack is missing
(a FOR has not been executed) or the memory address of the
loop variable specified by the FOR does not match the memory address of
the loop variable specified by the NEXT an error 28 will be generated.
SEE: NEXT and EXITTO