1
NOTE: For BS1's, change line 1 to
SYMBOL Reps = W0
and line 3 to
DEBUG Reps
2
2
e
1
NOTE: On the BS1, the loop will
continue until Counter has gone
past EndValue. The rollover error
will still occur if the BS1 cannot
determine if Counter went past
EndValue.
5: BASIC Stamp Command Reference – FOR...NEXT
Reps
VAR
FOR Reps = 0 TO 65535 STEP 3000
DEBUG DEC ? Reps
NEXT
The value of reps increases by 3000 each trip through the loop. As it
approaches the EndValue, an interesting thing happens; Reps is: 57000,
60000, 63000, 464, 3464... It passes the EndValue, rolls over and keeps
going. That's because the result of the calculation 63000 + 3000 exceeds the
maximum capacity of a 16-bit number and then rolls over to 464. When
the result of 464 is tested against the range ("Is Reps > 0 and is Reps <
65500?") it passes the test and the loop continues.
A similar symptom can be seen in a program who's EndValue is mistakenly
set higher than what the counter variable can hold. The example below
uses a byte-sized variable, but the EndValue is set to a number greater than
what will fit in a byte:
1
SYMBOL
Reps =
FOR Reps = 0 TO 300
DEBUG Reps
NEXT
-- or --
2
2
Reps
VAR
p
sx
FOR Reps = 0 TO 300
DEBUG DEC ? Reps
NEXT
Here, Reps is a byte variable; which can only hold the number range 0 to
255. The EndValue is set to 300, however; greater than 255. This code will
loop endlessly because when Reps is 255 and the FOR...NEXT loop adds 1,
Reps becomes 0 (bytes will rollover after 255 just like words will rollover
after 65535). The result, 0, is compared against the range (0 – 255) and it is
found to be within the range, so the FOR...NEXT loop continues.
It's important to realize that on the BS2, BS2e, BS2sx and BS2p, the test is
against the entire range, not just the EndValue. The code below is a slight
modification of the previous example (the StartValue is 10 instead of 0) and
will not loop endlessly.
WORD
B0
BYTE
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 121
' Counter for the loop.
' Each loop add 3000.
' Show reps in debug window.
' Counter for the loop.
' Each loop add 1.
' Show reps in debug window.
' Counter for the loop.
' Each loop add 1.
' Show reps in debug window.
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?