Specifications

BASIC Stamp II
Parallax, Inc. • BASIC Stamp Programming Manual 1.8 • Page 261
2
For...Next
FOR
variable = start to end {STEP stepVal} ... NEXT
Create a repeating loop that executes the program lines between For
and Next, incrementing or decrementing variable according to stepVal
until the value of the variable passes the end value.
Variable
is a bit, nib, byte or word variable used as a counter.
Start
is a variable or constant that specifies the initial value of the
variable.
End
is a variable or constant that specifies the end value of the
variable. When the value of the variable passes end, the For...Next
loop stops executing and the program goes on to the instruction
after Next.
StepVal
is an optional variable or constant by which the variable
increases or decreases with each trip through the For/Next loop.
If start is larger than end, PBASIC2 understands stepVal to be
negative, even though no minus sign is used.
Explanation
For...Next loops let your program execute a series of instructions for a
specified number of repetitions. In simplest form:
reps var nib ' Counter for the FOR/NEXT loop.
FOR reps = 1 to 3 ' Repeat with reps = 1, 2, 3.
debug "*" ' Each rep, put one * on the screen.
NEXT
Each time the For...Next loop above executes, the value of reps is
updated. See for yourself:
reps var nib ' Counter for the FOR/NEXT loop.
FOR reps = 1 to 10 ' Repeat with reps = 1, 2... 10.
debug dec ? reps ' Each rep, show values of reps.
NEXT
For...Next can also handle cases in which the start value is greater than
the end value. It makes the commonsense assumption that you want to
count down from start to end, like so:
reps var nib ' Counter for the FOR/NEXT loop.
FOR reps = 10 to 1 ' Repeat with reps = 10, 9...1.
debug dec ? reps ' Each rep, show values of reps.
NEXT