Positron Compiler Documentation

Boolean Logic Operators

Source: Positron8 Compiler User Manual, PDF page 77

The If-Then-Else-EndIf, While-Wend, and Repeat-Until conditions now support the logical operators ‘and’ and ‘or’.

The operators ‘and’ and ‘or’ join the results of two conditions to produce a single true/false result. ‘and’ and ‘or’ work the same as they do in everyday speech. Run the example below once with and (as shown) and again, substituting or for and: -

Device = 18F26K40
                                  ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
  Dim Var1 as Byte = 5
  Dim Var2 as Byte = 9
  If Var1 = 5 and Var2 = 10 Then Result_True
  Stop
Result_True:
  HRsoutLn "Result Is True."

The condition "Var1 = 5 and Var2 = 10" is not true. Although Var1 is 5, Var2 is not 10. and works just as it does in plain English, both conditions must be true for the statement to be true. or also works in a familiar way; if one or the other or both conditions are true, then the statement is true. Xor (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.

Parenthesis (or rather the lack of it!).

Every compiler has its quirky rules, and the Positron8 compiler is no exception. One of its quirks means that parenthesis is not supported in a Boolean condition, or indeed with any of the If-Then-Else-EndIf, While-Wend, and Repeat-Until conditions. Parenthesis in an expression within a condition is allowed however. So, for example, the expression: -

If (Var1 + 3) = 10 Then do something.
                                            Is allowed.

but: -

If((Var1 + 3) = 10) Then do something.
                                            Is not allowed.

The Boolean operators do have a precedence within a condition. The and operator has the highest priority, then the or. This means that a condition such as: -

If Var1 = 2 and Var2 = 3 or Var3 = 4 Then do something

Will compare Var1 and Var2 to see if the and condition is true. It will then see if the or condition is true, based on the result of the and condition.

Then directive always required.

The Positron8 compiler relies heavily on the Then part. Therefore, if the Then part of a condition is left out of the code listing, a Syntax Error will be produced.