IF...THEN...ELSE Example

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

Syntax: IF…THEN…ELSE

 

 

 

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.

' IF-THEN-ELSE.BS2
' The program below generates a series of 16-bit random numbers and tests
' each to determine whether they're evenly divisible by 3. If a number is
' evenly divisible by 3, then it is printed, otherwise, the program generates
' another random number. The program counts how many numbers it prints, and
' quits when this number reaches 10.

' {$STAMP BS2}
' {$PBASIC 2.5}                         ' version 2.5 required

sample          VAR     Word            ' Random number to be tested
hits            VAR     Nib             ' Number of hits
misses          VAR     Word            ' Number of misses


Setup:
  sample = 11500

Main:
  DO
    RANDOM sample                       ' Put a random number into sample
    IF ((sample // 3) = 0) THEN         ' divisible by 3?
      DEBUG DEC5 sample,                ' - yes, print value and message
            " is divisible by 3", CR
      hits = hits + 1                   ' count hits (divisble by 3)
    ELSE
      misses = misses + 1               ' count misses
    ENDIF
  LOOP UNTIL (hits = 10)                ' quit after 10 hits

  DEBUG CR,
        "All done.", CR, CR,            ' display results
        "Hits:    ", DEC hits, CR,
        "Misses:  ", DEC misses, CR,
        "Samples: ", DEC (hits + misses)
  END

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012