BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – DO...LOOP
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 177
Note that the WHILE test (loop runs WHILE Condition is True) and UNTIL
test (loop runs UNTIL Condition is True) can be interchanged, but they are
generally used as illustrated above.
Demo Program (DO-LOOP.bs2)
' DO-LOOP.bs2
' This program creates a little guessing game. It starts by creating
' a (pseudo) random number between 1 and 10. The inner loop will run
' until the answer is guessed or 10 tries have been attempted. The
' outer loop has no condition and will cause the inner loop code to
' run until the BASIC Stamp is reprogrammed.
' {$STAMP BS2}
' {$PBASIC 2.5}
rVal VAR Word ' random value
answer VAR Byte ' game answer
guess VAR Byte ' player guess
tries VAR Nib ' number of tries
Main:
DO
RANDOM rVal
answer = rVal.LOWBYTE */ 10 + 1 ' create 1 - 10 answer
tries = 0
DO ' get answer until out of tries
DEBUG CLS,
"Guess a number (1 - 10): "
DEBUGIN DEC guess ' get new guess
tries = tries + 1 ' update tries count
LOOP UNTIL ((tries = 10) OR (guess = answer))
IF (guess = answer) THEN ' test reason for loop end
DEBUG CR, "You got it!"
ELSE
DEBUG CR, "Sorry ... the answer was ", DEC answer, "."
ENDIF
PAUSE 1000
LOOP ' run again
END
NOTE: This example program can be
used with all BS2 models by changing
the $STAMP directive accordingly.
All
2