DO...LOOP Example

BS2 icon BS2e icon BS2sx icon BS2p icon BS2pe icon BS2px icon {PBASIC 2.5}

Syntax: DO…LOOP

 

 

 

NOTE: The example below is written for the BS2 and will run on any of the BS2-family modules. Modify the $STAMP directive (as required) before downloading to the BS2e, BS2sx, BS2p, BS2pe, or BS2px.

' DO-LOOP.BS2
' This program creates a little guessing game.  It starts by creating
' a (psuedo) 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 an number (1 - 10): ",
            CLREOL
      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

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012