EasyManua.ls Logo

Parallax BASIC Stamp 2e - Page 121

Default Icon
353 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
5: BASIC Stamp Command Reference FOR…NEXT
BASIC Stamp Programming Manual 2.0c www.parallaxinc.com Page 119
Stamps 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 ' Each repetition, put the number of the
NEXT ' repetition on the screen.
-- 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 ' Each repetition, put the number of the
NEXT ' repetition on the screen.
Note that the code for the BS2, BS2e, BS2sx and BS2p 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 the BS2, BS2e, BS2sx and BS2p would be treated as
its positive, two's compliment counterpart. For example, 1 in two’s
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 ' This will try to decrement 3 by 65535.
DEBUG DEC Reps, CR ' Each repetition, put the number of the
NEXT ' repetition on the screen.
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
two's compliment) effectively making the number 65532 (4 in two's
compliment) which is outside the range of the loop.
All the arguments in the FOR…NEXT command can be constants,
variables or expressions (on the BS2, BS2e, BS2sx and BS2p). This leads to
some interesting uses. For example, if you make the StartValue and
EndValue a variable, and change their values within the loop, you'll change
the behavior of the loop itself. Try the following:
1
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
USING VARIABLES AS ARGUMENTS.

Table of Contents

Related product manuals