The loop clause
3. The command NEXT adds 1 to the counter.
4. The current counter value is compared with the final counter
• If the current counter value doesn't exceed the final counter
value, steps (2), (3), and (4) are repeated .
• If the current counter value exceeds the final counter value,
the definite loop structure is completed, and program execu-
tion continues after the NEXT command.
In this example, steps (2), (3), and (4) are repeated four times. The
loop counter is first incremented from 1 to 2, then to 3, then to 4, and
then to 5. At this point it exceeds the final value 4, so the definite
loop structure ends. Note that step (1) is performed before any tests
are made, so the loop clause is always evaluated at least once. For
another example of START '" NEXT, see "FIB2 (Fibonacci Numbers,
Loop Version)" on page 248.
FOR counter ... NEXT
In many cases it's handy to use the current value of the counter as a
variable in the loop clause. To do so, replace START by FOR
The counter becomes a local variable with the specified name. As be-
fore, this manual follows the convention of writing local names in
lowercase letters to help you distinguish them from global names. The
following example puts the first five square integers on the stack.
1 5 FOR
x is executed only once. The sequence.-..
the loop clause, which is executed repeatedly.
26: Program Structures