# IF...THEN Examples

Syntax: IF…THEN

    ' 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

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.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.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

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012