Arduino – Boolean or Logical Operators
Introduction
Logical operators evaluate either one or two relational or logical statements. There are 3 logical operators in Arduino IDE:
[table id=32 /]
Logic OR (||) Operator
Structure
(Statement 1) || (Statement2)
The logic OR operator results in true if either Statement1 or Statement2 or both are true. If both the statements are false, then it will result in false. Below is its truth table:
[table id=9 /]
Below is an example showing how to use the logic OR operator:
Serial monitor output:
Result 1: 1
Result 2: 1
Result 3: 1
Result 4: 0
Logic AND (&&) Operator
Structure
(Statement 1) && (Statement2)
The logic AND operator gives true only if both Statement 1 and Statement2 are true. If either Statement1 or Statement2 or both are false, then it will result in false. Below if the truth table:
[table id=10 /]
Below is an example showing how to use the logic AND operator:
Serial monitor output:
Result 1: 1
Result 2: 0
Result 3: 0
Result 4: 0
Logic NOT (!) Operator
Structure
! Statement
The NOT operator checks whether the Statement evaluates to 0 or not. If it is 0 it results in true; otherwise, it results false.
Below is an example showing how to use the NOT operator:
Serial monitor output:
Result 1: 0
Result 2: 1
Conclusion
With this topic over, you have learned about all the different types of operators. In the next topic, we will see how to implement conditional programming.