1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

2.3 Flow of control

Applesoft programs when 'RUN' start at the lowst numbered program line
with no variables set and proceed sequentially through the program.
Variables' contents are retained after the program exits until the
program's code is changed. While Applesoft does not have while loops,
named subroutines or the like, you can do all of them with what
Applesoft does have to offer.

The most basic flow of control statement is the 'IF x THEN y' clause.
Expression x is evaluated, and if true, then the rest of the line is
parsed. If x is false, then control skips to the next line of code. No
parentheses are needed around the x clause, but they don't really
hurt. There is no else clause in the language, but it is easy to get
around that-- put a GOTO at the end of the IF line to skip the next
line(s) of code, where the "else" clause is.

'IF x THEN GOTO n' can also be written as 'IF x GOTO n' or 'IF x THEN
n', but this abbreviation can only be done for 'THEN GOTO', no other
statements.

'GOTO n' transfers control to line n, if it exists. [And an error if
it doesn't.] 'GOSUB n' goes to a subroutine, and 'RETURN' goes back to
the site of the most recent GOSUB. GOSUBs can be nested up to about 12
or 16 deep, so recursion should be avoided if possible. The 'POP'
statement cancels the last return address, and continues on to the
next statement. [Essentially changing the last GOSUB to a GOTO.]

GOSUBs should be balanced by RETURN or POP, as there is only a limited
amount of space to store return addresses. If you try and RETURN or
POP more times than you've GOSUB'd, you'll get an error.

'FOR varb = start TO end [STEP increment]' sets up a loop. You can
only loop over reals; the increment defaults to 1 unless specified.
The loop stops when 'varb' is greater than 'end' if 'increment' is
positive, or less than 'end' if 'increment' is negative. The counter
variable 'increment' can be zero, in which case this is an infinite
loop. It is legal to modify the loop variable in the middle of a loop.
The contents of a FOR loop are always executed at least once, since it
only exits from the NEXT statement.

In addition, the start, end and increment parameters are evaluated
only once, at the start of the loop, so any changes to them will not
be reflected.

NEXT [varb][,varb2[,varb3]]' adds the step to the looping variable and
goes back to the top of the FOR..NEXT loop if it should continue. The
specification of the variable is optional-- if omitted, Applesoft will
use the innermost active loop to deal with. Crossing loops such as
'FOR I=1 TO 5: FOR J=1 TO 3: NEXT I: NEXT J' will cause errors, and
generally are a bad idea. However, if specified with the innermost
loop variable to the left separated by commas, it is doable: 'FOR I=1
TO 5: FOR J=1 TO 3: NEXT J,I'

Loops should be terminated with a 'NEXT', not a GOTO, since there is a
limited amount of space for tracking active loops, and the loop is
still active. One recommended solution is to change the loop variable
to a terminal value, do the 'NEXT' and then do whatever you want.