The logical operators, And , Or , and Not are often used in conditional statements, but they can also be used in expressions where a Boolean result is required. Logical operators are defined only for Boolean operands (or expressions that evaluate to Boolean values).
The And and Or operators are optimizing; if the left operand is sufficient to determine the result, then the right operand will never be evaluated.
The following table shows examples of the logical operators.
Example | Result | Right Expression Evaluated? |
---|---|---|
(5 < 3) And (2 = 2) | False | No |
(5 > 3) And (2 = 2) | True | Yes |
(5 > 3) And (2 > 4) | False | Yes |
(5 > 3) Or (2 = 2) | True | No |
(5 < 3) Or (2 > 4) | False | Yes |
(5 < 3) Or (2 < 4) | True | Yes |
Not (2.0) | -2.0 | n/a |
Not (2.0) | -2.0 | n/a |
Not (2+2) | -4 | n/a |
Not (2^3) | -8.0 | n/a |
Not((5 < 3) Or (2 > 4)) | True | n/a |