Revised June 2021; see History
FOR <loopvar> = <startexpr> TO <endexpr> {STEP <stepexpr>}
<statements>
{IF <condition> REPEAT}
{IF <condition> EXIT}
NEXT <loopvar>
The FOR…NEXT loop executes the enclosed set of statements (aka the body) repeatedly until the value of loopvar (aka the loop counter) reaches the value of endexpr (the ending condition). More precisely, the terminating condition is met when loopvar >= endexpr (for positive stepexpr values), or loopvar <= endexpr (for negative stepexpr values). The body of the loop may optionally include REPEAT statements (which effectively jump to the NEXT statement to execute the next iteration of the loop, skipping the remaining statements in the body for this iteration), and EXIT statements (which terminate the loop immediately, as if the loopvar had hit the ending condition).
The loopvar must be either a floating point or integer (I) variable. If preceded by a % (e.g. %i or %counter), a temporary variable will be automatically created as an F,6, limited in scope to the loop and destroyed when the loop terminates. See History.
The startexpr and endexpr can be any numeric expressions that are compatible with the loopvar type. Do not, for example, try to use fractional starting or ending values with an integer loop variable. These expressions are only evaluated once, prior to the first test for the termination condition.
The optional stepexpr clause determines the amount by which the loopvar will be incremented at the end of each loop (default 1). As with the startexpr and endexpr, the type should be compatible with the loopvar.
Note: the test for the loop terminating condition may occur at the start or end of each iteration, depending on compiler switches. See Comments.
Examples
The following example illustrates a pair of FOR…NEXT loops, one nested inside the other. The inner loop uses an auto-generated loop variable (%j) whose scope is limited to the body of the inner loop. It also illustrates the EXIT statement, which exits from just the current loop, i.e. the inner loop, continuing with the next iteration of the outer loop.
for i = 1 to 10
for %j = i to 100 step 2
if fn’foo(i,%j) then
exit ! terminate inner loop
else
print i * %j
endif
next %j
next i
The next example illustrates a more complicated FOR statement, involving a STEP clause and calculations for startexpr and endexpr, both of which include the variable y, which is modified within the loop—but as noted in the source comment, that doesn’t affect the loop termination since the endexpr is only evaluated once.
for x = y*2 to fn’foo(y) step -1
if x = w exit ! terminate on this condition
if fn’bar(x) repeat ! skip to next iteration
y += z ! note this does not affect the loop terminating condition
next x
Comments
In contrast, when compiled with /X:1 or higher, or with /RC, the loopvar is checked against the endexpr before each iteration, so it is possible for the loop to execute zero times. Also block structuring is enforced, i.e. there must be a one-to-one physical relationship between the FOR and its subsequent NEXT statement. This variation matches the BASICPlus behavior and is more in line with other modern languages/implementations.
The REPEAT and EXIT statements require /X:1 or /RC.
See Also
History
2021 June, A-Shell 6.5.1704, compiler edit 946: the auto-mapped loopvar (e.g. %i, %loopvar, etc.) option introduced.