BASIC stamp manual v2.2
FOR…NEXT – BASIC Stamp Command Reference
Page 196 • BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com
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 all the BS2 models, 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.
reps VAR Byte ' counter for the loop
FOR reps = 10 TO 300 ' each loop add 1
DEBUG DEC ? reps ' show reps in Debug window
NEXT
reps still rolls over to 0, as before, however, this time it is outside the range
of 10 to 255. The loop stops, leaving reps at 0. Note that this code is still in
error since reps will never reach 300 until it is declared as a Word.
Demo Program (FOR-NEXT.bs1)
' FOR-NEXT.bs1
' This example uses a FOR...NEXT loop to churn out a series of sequential
' squares (numbers 1, 2, 3, 4... raised to the second power) by using a
' variable to set the FOR...NEXT StepValue, and incrementing StepValue
' within the loop. Sir Isaac Newton is generally credited with the
' discovery of this technique.
' {$STAMP BS1}
' {$PBASIC 1.0}
SYMBOL square = B2 ' FOR/NEXT counter
SYMBOL stepSize = B3 ' step size increases by 2 each loop
Setup:
stepSize = 1
square = 1
Main:
FOR square = 1 TO 250 STEP stepSize ' show squares up to 250
DEBUG square ' display on screen
stepSize = stepSize + 2 ' add 2 to stepSize
NEXT ' loop until square > 250
END
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.
All
2
1