# IF…THEN

{PBASIC 1.0} {PBASIC 2.0}

IF...THEN Examples

Syntax: IF Condition THEN Address

## Function

Evaluate Condition and, if it is true, go to the point in the program marked by Address.

- Condition is a statement, such as "x = 7" that can be evaluated as True or False. The Condition can be a very simple or very complex relationship, as described below.
- Address is a label that specifies where to go in the event that Condition is true.

Note: See IF…THEN…ELSE for all BS2 models and PBASIC 2.5.

## Quick Facts

| BS1 | BS2 Family
Comparison operators | =, <>, >, <, >=, <= | =, <>, >, <, >=, <=
Conditional Logic Operators | AND, OR | NOT, AND, OR, XOR
Format of Condition | Variable Comparison Value; where Value is a variable or constant | Value1 Comparison Value2; where Value1 and Value2 can be any of variable, constant or expression
Parentheses | Not Allowed | Allowed
Related Commands | None | IF…THEN…ELSE, SELECT...CASE

## Explanation

IF...THEN is PBASIC's primary decision maker. It tests a condition and, if that condition is true, goes to a point in the program specified by an address label. The condition that IF...THEN tests is written as a mixture of comparison and logic operators. The available comparison operators are:

Comparison Operator Symbol | Definition
= | Equal
<> | Not Equal
> | Greater Than
< | Less Than
>= | Greater Than or Equal To
<= | Less Than or Equal To

Comparisons are always written in the form: Value1 Comparison Value2. The values to be compared can be any combination of variables (any size), constants, or expressions.

Note: On the BS1, expressions are not allowed as arguments, and Value1 (to the left of comparison) must be a variable.

The following example is an IF...THEN command with a simple condition:

      IF 10 < 200 THEN Main

This code will compare the number 10 to the number 200. If 10 is less than 200, the condition is true. In this case, 10 is less than 200 (and always will be), so the program will jump (or GOTO) the label called Main. Of course, this is a silly example (10 is always less than 200 so this line will always cause a jump to Main). Most of the time, you'll use at least one variable in your condition:

    SYMBOL  value   = W1

    Main:
      PULSIN 0, 1, value
      DEBUG #value, CR
      IF value < 4000 THEN Main
      DEBUG "Value was greater than 4000!"
      END

    value   VAR     Word

    Main:
      PULSIN 0, 1, value
      DEBUG DEC value, CR
      IF (value < 4000) THEN Main
      DEBUG "Value was greater than 4000!"
      END

Here, the BASIC Stamp will look for and measure a pulse on I/O pin 0, then compare the result, value, against 4000. If value is less than (<) 4000, it will jump back to Main. Each time through the loop, it displays the measured value and once it is greater than or equal to 4000, it displays, "Value was greater than 4000!"

On the BS2, BS2e, BS2sx, BS2p, and BS2pe, the values can be expressions as well. This leads to very flexible and sophisticated comparisons. The IF...THEN statement below is functionally the same as the one in the program above:

      IF (value < (45 * 100 - (25 * 20))) THEN Main

Here the BASIC Stamp evaluates the expression: 45 * 100 = 4500, 25 * 20 = 500, and 4500 - 500 = 4000. Then the BASIC Stamp performs the comparison: is value < 4000? Another example that is functionally the same:

      IF ((value / 100) < 40) THEN Main

It's important to realize that all comparisons are performed using unsigned, 16-bit math. This can lead to strange results if you mix signed and unsigned numbers in IF...THEN conditions. Watch what happens here when we include a signed number (-99):

      IF (-99 < 100) THEN Is_Less
      DEBUG  "Greater than or equal to 100"
      END

    Is_Less:
      DEBUG "Less than 100"
      END

Although -99 is obviously less than 100, the program will say it is greater. The problem is that -99 is internally represented as the two's complement value 65437, which (using unsigned math) is greater than 100. This phenomena will occur whether or not the negative value is a constant, variable or expression.

IF...THEN supports the conditional logic operators NOT, AND, OR, and XOR. See the table below for a list of the operators and their effects. NOTE: The NOT and XOR operators are not available on the BS1.

The NOT operator inverts the outcome of a condition, changing False to True, and True to False. The following IF...THENs are equivalent:

      IF (x <> 100) THEN Not_Equal                  ' Jump to Not_Equal if x is not 100
      IF NOT (x = 100) THEN Not_Equal               ' Jump to Not_Equal if x is not 100

    SYMBOL  value1  = B2
    SYMBOL  value2  = B3

    Setup:
      value1 = 5
      value2 = 9

    Main:
      IF value1 = 5 AND value2 = 10 THEN Is_True    ' change AND to OR and see
      DEBUG  "Statement was false."                 '   what happens
      END

    Is_True:
      DEBUG "Statement was true."
      END

    value1  VAR     Byte
    value2  VAR     Byte

    Setup:
      value1 = 5
      value2 = 9

    Main:
      ' Change AND to OR and see what happens
      IF (value1 = 5) AND (value2 = 10) THEN Is_True
      DEBUG  "Statement was false."
      END

    Is_True:
      DEBUG "Statement was true."
      END

The condition "(value1 = 5) AND (value2 = 10)" is not true. Although value1 is 5, value2 is not 10. The AND operator works just as it does in English; both conditions must be true for the statement to be true. The OR operator also works in a familiar way; if one or the other or both conditions are true, then the statement is true. The XOR operator (short for exclusive-OR) may not be familiar, but it does have an English counterpart: If one condition or the other (but not both) is true, then the statement is true.

The table below summarizes the effects of the conditional logic operators. As with math, you can alter the order in which comparisons and logical operations are performed by using parentheses. Operations are normally evaluated left-to-right. Putting parentheses around an operation forces PBASIC 2.0 to evaluate it before operations not in parentheses.

Note: On the BS1, parentheses are not allowed within arguments.

Condition A | NOT A
False | True
True | False

Condition A | Condition B | A AND B
False | False | False
False | True | False
True | False | False
True | True | True

Condition A | Condition B | A OR B
False | False | False
False | True | True
True | False | True
True | True | True

Condition A | Condition B | A XOR B
False | False | False
False | True | True
True | False | True
True | True | False

Unlike the IF...THEN commands in other BASIC language variants, PBASIC 1.0/2.0's IF...THEN can only go to a label as the result of a decision. It cannot conditionally perform some instruction, as in "IF x < 20 THEN y = y + 1" (This is possible using the $PBASIC 2.5 directive). To achieve this in PBASIC 1.0 and 2.0, you have to invert the logic using NOT and skip over the conditional instruction unless the condition is met:

      IF NOT (x < 20) THEN No_Inc           ' Don't increment y unless x < 20.
      y = y + 1                             ' Increment y if x < 20.

    No_Inc:                                 ' Program continues.

You can also code a conditional GOSUB, as in "IF (x = 100) THEN GOSUB Centennial." In PBASIC 2.0:

      IF NOT (x = 100) THEN No_Cent
      GOSUB Centennial                      ' IF x = 100 THEN GOSUB Centennial.

    No_Cent:                                ' Program continues.

Internally, the BASIC Stamp defines "False" as 0 and "True" as any value other than 0. Consider the following instructions:

    flag    VAR     Bit

    Setup:
      flag = 1

    Test:
      IF flag THEN Is_True

    Is_False:
      DEBUG "False", CR
      END

    Is_True:
      DEBUG "True"
      END

Since flag is 1, IF...THEN would evaluate it as true and print the message "True" on the screen. Suppose you changed the IF...THEN command to read "IF NOT (flag) THEN IsTrue." That would also evaluate as True. Whoa! Isn't NOT 1 the same thing as 0? No, at least not in the 16-bit world of the BASIC Stamp.

Internally, the BASIC Stamp sees a bit variable containing 1 as the 16-bit number %0000000000000001. So it sees the NOT of that as %1111111111111110. Since any non-zero number is regarded as True, NOT 1 is True. Strange but true.

The easiest way to avoid the kinds of problems this might cause is to always use a conditional operator with IF...THEN. Change the example above to read IF (flag = 1) THEN Is_True. The result of the comparison will follow IF...THENrules. Also, the logical operators will work as they should; IF NOT (flag = 1) THEN Is_True will correctly evaluate to False when flag contains 1.

This also means that you should only use the "named" conditional logic operators NOT, AND, OR, and XOR with IF...THEN. The conditional logic operators format their results correctly for IF...THEN instructions. The other logical operators, represented by symbols ~ & | and ^ do not; they are binary logic operators.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012