# FOR…NEXT
> [HTML Version](for_next_ctrl_struct.htm)
_Revised June 2021; see History_
**FOR = TO \{STEP \}**
\{IF REPEAT\}
\{IF EXIT\}
**NEXT **
****
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**
• [Auto-Mapped %Var Variables](auto-mappedvarvariables.htm.md)
**History**
2021 June, A-Shell 6.5.1704, compiler edit 946: the auto-mapped _loopvar_ (e.g. %i, %loopvar, etc.) option introduced.