FOR…NEXT - BASIC Stamp Command Reference
Page 118
•
BASIC Stamp Programming Manual 2.0b
•
www.parallaxinc.com
Explanation
FOR...NEXT loops let your program execute a series of instructions for a
specified number of repetitions (called iterations). By default, each time
through the loop, the counter variable is incremented by 1. It will
continue to loop until the result of the counter is outside of the range set
by StartValue and EndValue. Also, FOR…NEXT loops always execute at
least once. The simplest form is shown here:
Reps VAR NIB ' Counter for the FOR/NEXT loop.
FOR Reps = 1 TO 3 ' Repeat with Reps = 1, 2, 3.
DEBUG "*" ' Each repetition, put one * on the screen.
NEXT
In the above code, the FOR command sets Reps = 1. Then the DEBUG line
(within the FOR…NEXT loop) is executed; printing an asterisk (*) on the
screen. When the BASIC Stamp sees the NEXT command, it goes back to
the previous FOR command, adds 1 to Reps and compares the result to the
range set by StartValue and EndValue. If Reps is still within range, it
executes the code in the loop again. Each time the FOR...NEXT loop
executes, the value of Reps is updated (incremented by 1) and the code
within the loop (the DEBUG line) is executed; printing another asterisk on
the screen. This code will run through the loop three times; setting Reps to
1, 2 and 3, and printing three asterisks on the screen. After the third loop,
again the BASIC Stamp goes back up to the FOR command, adds 1 to Reps
and compares the result (4 in this case) to the range. Since the range is 1 to
3 and the value is 4 (outside the range) the FOR…NEXT loop is done and
the BASIC Stamp will jump down to the first line of code following the
NEXT command.
You can view the changing values of Reps by including the Reps variable in
a DEBUG command within the loop:
Reps VAR NIB ' Counter for the FOR/NEXT loop.
FOR Reps = 1 TO 3 ' Repeat with Reps = 1, 2, 3.
DEBUG DEC Reps, CR ' Each repetition, put the number of the
NEXT ' repetition on the screen.
Running this example should display "1" , "2", and "3" on the screen.
FOR…NEXT 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
1
NOTE: Replace the first line with
SYMBOL Reps = B0
on the BS1.
NOTE: Change the first line as
noted above and replace line 3 with
DEBUG #Reps, CR
on the BS1.
1
S
IMPLEST FORM OF
FOR…NEXT.
D
ECREMENTING THE COUNTER
INSTEAD OF INCREMENTING IT
.
P
ROCESSING A
FOR…NEXT
LOOP
.
1
NOTE: On the BS1, the loop will
continue until Counter has gone
past EndValue.
Содержание BASIC Stamp 1
Страница 1: ...BASIC Stamp Programming Manual Version 2 0c...
Страница 30: ...Introduction to the BASIC Stamps Page 28 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 90: ...BUTTON BASIC Stamp Command Reference Page 88 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 118: ...END BASIC Stamp Command Reference Page 116 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 128: ...FREQOUT BASIC Stamp Command Reference Page 126 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 196: ...NAP BASIC Stamp Command Reference Page 194 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 206: ...OWIN BASIC Stamp Command Reference Page 204 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 214: ...OWOUT BASIC Stamp Command Reference Page 212 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 216: ...PAUSE BASIC Stamp Command Reference Page 214 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 226: ...POLLMODE BASIC Stamp Command Reference Page 224 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 232: ...POLLOUT BASIC Stamp Command Reference Page 230 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 236: ...POLLRUN BASIC Stamp Command Reference Page 234 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 240: ...POLLWAIT BASIC Stamp Command Reference Page 238 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 262: ...RCTIME BASIC Stamp Command Reference Page 260 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 274: ...RUN BASIC Stamp Command Reference Page 272 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 310: ...SEROUT BASIC Stamp Command Reference Page 308 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 324: ...STOP BASIC Stamp Command Reference Page 322 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...