Please enable JavaScript to view this site.

A-Shell Reference

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, and the expression is evaluated only once.

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

When compiled with OCMPIL or COMPIL without one of the /X or /RC switches, the loopvar is checked against the endexpr after each iteration, so the loop always executes at least once, even if the startexpr exceeds the endexpr at the very start. Also, in this mode, there is no source level enforcement of block structuring, i.e. there is no physical limitation on the relationship between the FOR statement and its associated NEXT statement(s). This variation exactly matches AlphaBASIC, down to the byte codes in the RUN file.

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.