BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – IF...THEN
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 241
' {$STAMP BS2}
' {$PBASIC 2.0}
sample VAR Word ' Random number to be tested
samps VAR Nib ' Number of samples taken
temp VAR Nib ' Temporary workspace
Setup:
sample = 11500
Mult3:
RANDOM sample ' Put a random number into sample
temp = sample // 3
IF temp <> 0 THEN Mult3 ' Not multiple of 3? -- try again
DEBUG DEC5 sample, " divides by 3", CR
samps = samps + 1 ' Count multiples of 3
IF samps = 10 THEN Done ' Quit with 10 samples
GOTO Mult3 ' keep checking
Done:
DEBUG CR, "All done."
END
Demo Program (IF-THEN-ELSE.bs2)
' 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
All
2
NOTE: This example program can be
used with all BS2 models by changing
the $STAMP directive accordingly.