Original Author Paul Laughton, 2011
Page 80
De Re BASIC!
Initially, <nvar> is assigned the value of <nexp_1> and compared to <nexp_2>. {STEP <nexp_3>} is
optional and may be omitted. If omitted then the Step value is 1.
If <nexp_3> is positive then
if <nvar> <= <nexp_2> then
the statements between the For and Next are executed.
If <nexp_3> is negative then
if <nvar> >= <nexp_2> then
the statements between the For and Next are executed.
When the Next statement is executed, <nvar> is incremented or decremented by the Step value and the
test is repeated. The <statement>s will be executed as long as the test is true. Each time, <nvar> is
compared to the original value of <nexp_2>; <nexp_2> is not re-evaluated with each Next.
Because the keywords To and Step are in the middle of the line with expressions that may include
variables, it is possible to confuse BASIC!. Remember that the interpreter does not see any spaces you
put between variables and keywords. FOR a TO m is seen as foratom. If there is any possibility of
confusion, use parentheses to tell BASIC! that a name is a variable:
FOR WinTop TO WinBot
% ERROR: interpreted as "FOR win TO ptowinbot"
FOR (WinTop) TO WinBot
% interpreted as intended
For-Next loops can be nested to any level. When For-Next loops are nested, any executed Next
statement will apply to the currently executing For statement. This is true no matter what the <nvar>
coded with the Next is. For all practical purposes, the <nvar> coded with the Next should be considered
to be nothing more than a comment.
It is possible to exit a For loop without Next or F_N.break. However, this can create subtle logic errors
that are hard to debug. If you set debug mode (see the Debug.on command), then BASIC! can help you
find these bugs. When your program ends and debug is on, if your program entered a For loop and did
not leave it cleanly, BASIC! shows a run-time error: "Program ended with FOR without NEXT".
F_N.continue
If this statement is executed within a For-Next loop, the rest of the current pass of the loop is skipped.
The Next statement executes immediately.
F_N.break
If this statement is executed within a For-Next loop, the rest of the current pass of the loop is skipped
and the loop is terminated. The statement immediately following the Next will be executed.
While <lexp> / Repeat
While <lexp>
<statement>