BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – FOR...NEXT
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 193
reps VAR Nib ' counter for the FOR/NEXT loop
FOR reps = 1 TO 3 ' repeat with reps = 1, 2, 3
DEBUG DEC reps, CR ' print rep number
NEXT
Running this example should display "1" , "2", and "3" on the screen.
FORNEXT can also be made to decrement (rather than increment) the
Counter variable. The BS1 does this when you specify a negative StepValue
(as well as a StartValue that is greater than the EndValue). All other BASIC
Stamp models do this automatically when the StartValue is greater than
the EndValue. Examples of both are shown below:
SYMBOL reps = B0 ' counter for the FOR/NEXT loop
FOR reps = 3 TO 1 STEP -1 ' repeat with reps = 3, 2, 1
DEBUG #reps, CR ' print reps number
NEXT
-- or --
reps VAR Nib ' counter for the FOR/NEXT loop
FOR reps = 3 TO 1 ' repeat with reps = 3, 2, 1
DEBUG DEC reps, CR ' print reps number
NEXT
Note that the code for all the BS2 models did not use the optional STEP
argument. This is because we wanted to decrement by positive 1 anyway
(the default unit) and the BASIC Stamp realizes it needs to decrement
because the StartValue is greater than the EndValue. A negative StepValue
on any BS2 model would be treated as its positive, twos complement
counterpart. For example, –1 in twos complement is 65535. So the
following code executes only once:
reps VAR Nib ' counter for the FOR/NEXT loop
FOR reps = 3 TO 1 STEP -1 ' try to decrement 3 by 65535
DEBUG DEC reps, CR ' print reps number
NEXT
The above code would run through the loop once with reps set to 3. The
second time around, it would decrement reps by 65535 (-1 is 65535 in twos
complement) effectively making the number –65532 (4 in twos
complement) which is outside the range of the loop.
DECREMENTING THE COUNTER INSTEAD
OF INCREMENTING IT
.
1
All
2
All
2
All
2
NOTE: Change the first line as noted
above and replace line 3 with
DEBUG #Reps, CR
1