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

  1. Evaluate first condition.
  2. If first is true, result is already true.
  3. If first is false, evaluate the next condition.
  4. 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 ||

ABA || B
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

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 with if.
if (isHot || buttonPressed) { ... }  // logical
int flags = value | 0x04;              // bitwise

When 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

  1. Read button and convert it to boolean buttonPressed.
  2. Check temperature threshold and store result in isHot.
  3. Use manualOverride || buttonPressed || isHot to combine three possible ON triggers.
  4. If any trigger is true, LED turns ON and Serial prints ON reason.
  5. If all are false, else branch turns LED OFF.

What Happens Inside

  1. CPU evaluates first boolean condition.
  2. If first is true, full OR expression becomes true immediately.
  3. Otherwise CPU checks next condition(s).
  4. Branch is selected from final OR result.
  5. 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.