Operators
Lesson 37: Using || Logical OR Boolean Operator
Learn how || combines conditions and returns true when at least one condition is true.
Progress indicator
Lesson 37 of 57
Learning Objectives
- Understand what logical OR (||) does.
- Use || to combine multiple conditions in if statements.
- Read the truth table for || and apply it in Arduino logic.
- Understand || vs | and when each is correct.
- Avoid common mistakes with OR-based decisions.
Concept Explanation
What is the Logical OR Operator (||)
The || operator combines conditions and returns true if at least one condition is true.
It returns false only when every condition is false.
Logical OR Syntax
if (conditionA || conditionB) {
// runs if any one condition is true
}
if (a || b || c) {
// runs if at least one condition is true
}How || Works
- Evaluate first condition.
- If first is true, result is already true.
- If first is false, evaluate the next condition.
- Continue until one condition is true or all are checked.
This also uses short-circuit behavior. Once result is true, later checks may be skipped.
Truth Table for ||
| A | B | A || B |
|---|---|---|
| false | false | false |
| false | true | true |
| true | false | true |
| true | true | true |
Using || with Conditions
OR is useful when any one condition is enough to trigger an action. For example, alarm ON when smoke detected OR manual emergency switch pressed.
|| vs | (Comparison)
||is logical OR for boolean conditions.|is bitwise OR for bit-level operations.- Use
||in decision making withif.
if (isHot || buttonPressed) { ... } // logical
int flags = value | 0x04; // bitwiseWhen to Use ||
- When any one event should trigger output.
- For backup/fallback conditions.
- For manual override OR automatic trigger designs.
- For safety alerts from multiple sources.
Example Code
LED turns ON when manual override OR button press OR high temperature is detected.
const int LED_PIN = 2;
const int BUTTON_PIN = 0;
const int TEMP_THRESHOLD = 30;
bool manualOverride = false;
int buttonState = HIGH;
int temperatureC = 28;
bool buttonPressed = false;
bool isHot = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
buttonState = digitalRead(BUTTON_PIN);
buttonPressed = (buttonState == LOW);
isHot = (temperatureC >= TEMP_THRESHOLD);
if (manualOverride || buttonPressed || isHot) {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED ON: at least one condition is true");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("LED OFF: all conditions are false");
}
delay(300);
}Example Code Explanation
- Read button and convert it to boolean
buttonPressed. - Check temperature threshold and store result in
isHot. - Use
manualOverride || buttonPressed || isHotto combine three possible ON triggers. - If any trigger is true, LED turns ON and Serial prints ON reason.
- If all are false, else branch turns LED OFF.
What Happens Inside
- CPU evaluates first boolean condition.
- If first is true, full OR expression becomes true immediately.
- Otherwise CPU checks next condition(s).
- Branch is selected from final OR result.
- Output and logs are updated based on selected branch.
Common Mistakes with ||
- Using
|instead of||in conditions. - Assuming all conditions must be true (that is AND behavior, not OR).
- Forgetting parentheses in mixed && and || expressions.
Best Practices for ||
- Use clear boolean names like
isHot,buttonPressed. - Put most likely true checks first for short-circuit efficiency.
- Use parentheses for mixed logic to avoid ambiguity.