Statement FOR
Start a counted integer loop.
Syntax
FOR variable = start TO limit [STEP increment]Parameters
| Parameter | Meaning |
|---|---|
variable | Loop variable A–Z. |
start | Initial expression assigned to the variable. |
limit | Final expression, evaluated at loop entry. |
increment | Step expression, evaluated at entry; default 1, zero is an error. |
Behavior
The loop includes the limit when the step reaches it exactly. Positive steps continue while the variable is at most the limit; negative steps continue while it is at least the limit.
If the initial range is empty, the runtime skips to the corresponding NEXT, including nested FOR/NEXT pairs, without executing the body. Otherwise a frame stores the loop variable, body location, limit and step.
At NEXT the variable is incremented and compared again. Signed overflow of that increment ends the loop. FOR and GOSUB share a maximum of eight active frames.
Example
10 FOR I=3 TO 1 STEP -1
20 PRINT I
30 NEXT I
40 ENDLimits and diagnostics
NEXT must name the matching variable, and active loops must close in stack order. No variable lists, EXIT FOR or implicit stack unwinding on GOTO.
See also
NEXT · GOSUB · LET · Language syntax