Introduction
Logical operators evaluate either one or two relational or logical statements. There are 3 logical operators in Arduino IDE:
Logical Operator | Operator Symbol | Example |
---|---|---|
OR | || | a || b |
AND | && | a && b |
NOT | ! | ! a |
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:
Statement1 | Statement2 | Statement1 || Statemen2 |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
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:
Statement1 | Statement2 | Statement1 || Statemen2 |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
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