Tutorial: Logical Operators
What is a Logical Operator?
A logical operator gives you more freedom in your boolean expressions. Because of this, they are sometimes called boolean operators.
The format for using logical operators is :
boolExp LogOp boolExp
To learn more about boolean expressions, see our Boolean Expressions tutorial.
And
A boolean expression that uses AND is true if both expressions on both side of the and are true.
For example :
If True And True Then Print "Both are true."
If True And False Then Print "This won't be displayed."
Sync
WaitKey
| AND Truth Table | ||
|---|---|---|
| Boolean 1 | Boolean 2 | Result |
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
Or
A boolean expression using Or is true if one or both of the expressions on each side of Or are true.
For example :
If False Or False Then Print "Neither are true, so this won't be displayed."
If False Or True Then Print "One is true, so this is displayed."
If True Or False Then Print "One is true, so this is displayed."
If True Or True Then Print "Both are true. This is displayed because at least one is."
Sync
WaitKey
| OR Truth Table | ||
|---|---|---|
| Boolean 1 | Boolean 2 | Result |
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
XOr
A boolean expression using XOr is true if one or the other the expressions on both sides are True.
If False XOr False Then Print "False XOr False : False"
If False XOr True Then Print "False XOr True : True"
If True XOr False Then Print "True XOr False : True"
If True XOr False Then Print "True XOr True : False."
Sync
WaitKey
| OR Truth Table | ||
|---|---|---|
| Boolean 1 | Boolean 2 | Result |
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | False |
Not
Not logically reverses True and False.
If Not True Then Print "Not True: False"
If Not False Then Print "Not False: True"
Sync
WaitKey
Next Steps
| Categories: Beginner Tutorials : Programming Tutorials : Tutorials |