2
2
e
1
NOTE: Expressions are not
allowed as arguments on the BS1.
1
NOTE: Use a minus sign to
indicate negative StepValues on the
BS1.
Table 5.11: FOR...NEXT Quick
Facts.
5: BASIC Stamp Command Reference – FOR...NEXT
FOR...NEXT
1
FOR Counter = StartValue TO EndValue {STEP {-} StepValue} ... NEXT {Counter}
2
2
FOR Counter = StartValue TO EndValue {STEP StepValue} ... NEXT
sx
p
Function
Create a repeating loop that executes the program lines between FOR and
NEXT, incrementing or decrementing Counter according to StepValue until
the value of the Counter variable passes the EndValue.
• Counter is a variable (usually a byte or a word) used as a counter.
• StartValue is a variable/constant/expression (0 – 65535) that
specifies the initial value of the variable (Counter).
• EndValue is a variable/constant/expression (0 – 65535) that specifies
the end value of the variable (Counter). When the value of Counter is
outside of the range StartValue to EndValue, the FOR...NEXT loop
stops executing and the program goes on to the instruction after
NEXT.
• StepValue is an optional variable/constant/expression (0 – 65535) by
which the Counter increases or decreases with each iteration through
the FOR...NEXT loop. On the BS1, use a minus sign (-) in front of
the StepValue to indicate a negative step.
Stamps, if StartValue is larger than EndValue, PBASIC understands
StepValue to be negative, even though no minus sign is used.
Quick Facts
Max. nested
commands
To decrement
counter
variable
Counter
comparison
* Direction (ie: increment/decrement) cannot be changed at runtime.
BS1
BS2
BS2e BS2sx BS2p
BS1
8
Set StartValue > EndValue
and enter negative
StepValue*
Exit loop if Counter exceeds
EndValue
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 117
On all other BASIC
BS2, BS2e, BS2sx and BS2p
16
Set StartValue > EndValue
Exit loop if Counter outside of range set by
StartValue to EndValue
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?