FOR <loopvar> = <startval> to <endval> {step <stepval>}
<statements>
{IF <condition> repeat}
{IF <condition> exit}
{<statements>}
NEXT <loopvar>
The for...next loop executes a set of statements repeatedly until the loop counter hits (or surpasses) the ending condition or the exit statement is encountered. For positive stepval values, this will occur when the loopvar >= endval, whereas when the stepval is negative, it will occur when the loopvar <= endval. The statements in the body of the loop may optionally include repeat statements (which effectively jump to the next statement in order to execute next iteration of the loop, skipping the remaining statements in the body for this iteration), and exit statements (which terminate the loop, as if the loopvar had hit the endval).
The loopvar must be either a floating point or integer (I) variable. The startval and endval can be any numeric expressions that are compatible with the loopvar type. (Don't, for example, try to use fractional starting or ending values with an integer loop variable.) Also, considering that the endval expression will be evaluated for each iteration of the loop, unless the expression's value changes during execution of the loop, for efficiency reasons it is best to pre-evaluate and assign it to a variable, rather than redundantly evaluating it each time.
The optional step clause determines the amount by which the loopvar will be incremented at the end of each loop (default 1). As with the startval and endval, the type should be compatible with the loopvar.
Compatibility: Supported since the original AlphaBASIC, but the semantics were changed in BASIC Plus—i.e. /X:1 and above, and also /RC. In the old compile mode, without /X:1 or /RC, A-Shell implementation matches the AMOS version at both the source and RUN level. In the BASIC Plus mode and above, the A-Shell implementation matches BASIC Plus as the source level only. The repeat and exit statements require /X:1 or /RC.
Example
for x = y*2 to z step -1
if x = w exit ! terminate on this condition
if x mod 2 then repeat ! skip to next iteration
z = fn'mystery(x)
next x
The above example is not one to emulate, since it is completely mysterious what the loop does or when or even if it will ever terminate. But it does illustrate that it is possible (however ill-advised) to modify the end condition within the loop body.
Comments
In the original implementation (without /X:1 or /RC), the condition increment operation and test for termination occurs at the end of the loop, so the loop always executes at least once, regardless of the startval and endval. Furthermore, the loop was not block-structured; the next statement could have been placed anywhere relative to the loop (and you could even have multiple next statements). Not that any of that flexibility encouraged good code.). The repeat and exit statement were not supported.
In the BASIC Plus implementation (/X:1 or /RC), the loop is block-structured, meaning that the loop body must be physically between the for and next statements. Furthermore, the termination condition is checked at the top of the loop, so it is possible for the loop to execute zero times.