Boolean Logic Operators
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 = 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be running at 16MHz
Declare Hserial1_Baud = 9600
Declare Hrsout1_Pin = PORTB.14
Dim MyByte1 as Byte
Dim MyByte2 as Byte
PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX) ' Make Pin RB14 U1TX
MyByte1 = 5
MyByte2 = 9
If MyByte1 = 5 And MyByte2 = 10 Then GoTo Res_True
Stop
Res_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.
Parenthesis (or rather the lack of it!).
Every compiler has its quirky rules, and the Positron16 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 operands do have a precedence within a condition. The and operator has the highest priority, then the or, then the xor. 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 always required.
The Positron16 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.