Page 74 · Robotics with the Boe-Bot
value = value - anotherValue ' Answer = -1500
DEBUG "value = ", SDEC value, CR ' Display values again
√ Run the modified program and verify that value changes from 500 to -1500.
Counting and Controlling Repetitions
The most convenient way to control the number of times a piece of code is executed is
with a FOR…NEXT loop. Here is the syntax:
FOR Counter = StartValue TO EndValue {STEP StepValue}…NEXT
The three-dots ... indicate that you can put one or more commands between the FOR
and NEXT statements. Make sure to declare a variable for use in the Counter argument.
The
StartValue and EndValue arguments can be either numbers or variables. When
you see something between curly braces { } in a syntax description, it means it’s an
optional argument. In other words, the
FOR…NEXT loop will work without it, but you can
use it for a special purpose.
You don’t have to name the variable “counter”. For example, you can call it
“myCounter”.
myCounter VAR Word
Here’s an example of a FOR…NEXT loop that uses the myCounter variable for counting. It
also displays the value of the
myCounter variable each time through the loop.
FOR myCounter = 1 TO 10
DEBUG ? myCounter
PAUSE 500
NEXT
Example Program: CountToTen.bs2
√ Enter, save, and run CountToTen.bs2.
' Robotics with the Boe-Bot – CountToTen.bs2
' Use a variable in a FOR...NEXT loop.
' {$STAMP BS2}
' {$PBASIC 2.5}
myCounter VAR Word