BASIC stamp manual v2.2
5: BASIC Stamp Command Reference – IF...THEN
BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com • Page 237
In addition to supporting everything discussed above, PBASIC 2.5
provides enhancements to the IF…THEN command that allow for more
powerful, structured programming. In prior examples we’ve only used
the first syntax form of this command: IF Condition(s) THEN Address. That
form, while handy in some situations, can be quite limiting in others. For
example, it is common to need to perform a single instruction based on a
condition. Take a look at the following code:
' {$PBASIC 2.5}
x VAR Byte
FOR x = 1 TO 20 ' count to 20
DEBUG CR, DEC x ' display num
IF (x // 2) = 0 THEN DEBUG " EVEN" ' even num?
NEXT
This example prints the numbers 1 through 20 on the screen but every
even number is also marked with the text “ EVEN.” The IF…THEN
command checks to see if x is even or odd and, if it is even (i.e.: x // 2 = 0),
then it executes the statement to the right of THEN: DEBUG “ EVEN.” If it
was odd, it simply continued at the following line, NEXT.
Suppose you also wanted to mark the odd numbers. You could take
advantage of the optional ELSE clause, as in:
' {$PBASIC 2.5}
x VAR Byte
FOR x = 1 TO 20 ' count to 20
DEBUG CR, DEC x
IF (x // 2) = 0 THEN DEBUG " EVEN" ELSE DEBUG “ ODD”
NEXT
This example prints the numbers 1 through 20 with “ EVEN” or “ ODD”
to the right of each number. For each number (each time through the
loop) IF…THEN asks the question, “Is the number even?” and if it is it
executes DEBUG “ EVEN” (the instruction after THEN) or, if it is not even
it executes DEBUG “ ODD” (the instruction after ELSE). It’s important to
note that this form of IF…THEN always executes code as a result of
Condition(s); it either does “this” (THEN) or “that” (ELSE).
IF…THEN WITH A SINGLE STATEMENT