FOR and NEXT (Command)

V1
Indications are,  and   in SmileBasic V1 are identical to those in V2.

V2
and  are commands that cause the code between them to be run one or more times. is followed by a variable name, the  symbol, a numerical expression, the   keyword, and another numerical expression (and optionally, the   keyword and a third numerical expression). is either a complete command by itself, or it is followed by a variable name.

Most commonly, it is used like the following:

The interior of the loop is executed once with the variable  containing the value 1, then it is executed once with   equal to 2, etc., and on its final loop   is equal to 10. If the interior of the loop were, the loop as a whole would print all the integers from 1 to 10 inclusive. This makes it easy to initialize an array:

If you wish to 'count by' some value other than 1, use the  option:

It can also count down, using a negative  value:

Technically, when a  is encountered, the location in program memory is added to a stack, the expression before   is evaluated, and that value is assigned to the variable.

When a  is encountered, and there is a variable name after it, the variable name at the   at the top of the stack is compared against it. If there is a mismatch of variable names, the system generates a  error.

If there is no variable name mismatch, the expression after  is evaluated (or if there is no , the value 1 is used), and added to the variable. The expression after  is evaluated. If the  value is zero or positive and the variable is not greater than the   value, the program goes back to the command just after the. If the  value is negative and the variable is not less than the   value, the program also goes back to the command just after the. If neither of these conditions apply, the top of the stack is removed, and the program continues at the command after the.

It is worth commenting on the consequences of the  and   expressions being evaluated each loop. Evaluating the  each time means the following code:

will not print 5 times, as you might expect from just looking at the first two lines; it will print 9 times. Evaluating the  each time allows for a neat structure rather like a REPEAT loop - where another dialect of BASIC might have

SmileBasic can have

At the end of each loop,  is evaluated, and if it has the boolean value   (i.e. 0), the loop will run again, and if   evaluates to   (i.e. 1), the loop ends.

Of course, nested loops usually must have unique variable names. For instance, consider the code:

Unless  is otherwise modified in the inner loop, at Point A,   will always be 6, and the outer loop will never terminate.

The stack space is only 255 elements deep, so at the very beginning of a program, you may have 255 consecutive s, but the 256th will cause an   error. If a  is encountered when the  -stack is empty, the system will generate a   error.

The  stack memory is shared with the   stack memory... so, if the very start of a program is a , you can only run 254 consecutive  s within that subroutine before getting an   error. Any additions to the  stack made in a subroutine, that are not removed in the normal way by the execution of a   ending the loop, are removed by the. So, a single  can free up two or more spaces in the stack memory. In contrast, additions to the call stack made during a  loop are not removed by a.

V3
No information on SmileBasic V3.