BASIC stamp manual v2.2

IF…THEN – BASIC Stamp Command Reference
Page 240 BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com
Note that an IF…THEN construct can have as many as sixteen (16) ELSEIF
clauses, but a maximum of only one (1) ELSE clause.
There are three demo programs below. The first two demonstrate the
PBASIC 1.0 (BS1) and PBASIC 2.0 (all BS2 models) form of the IF…THEN
command. The last example demonstrates the PBASIC 2.5 (all BS2
models) form of IF…THEN.
Demo Program (IF-THEN.bs1)
' IF-THEN.bs1
' 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 BS1}
' {$PBASIC 1.0}
SYMBOL sample = W0 ' Random number to be tested
SYMBOL samps = B2 ' Number of samples taken
SYMBOL temp = B3 ' 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 #sample, "divides by 3", CR ' show sample divisible by 3
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.bs2)
' IF-THEN.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.
1
All
2
NOTE: This example program can be
used with all BS2 models by changing
the $STAMP directive accordingly.