Course contentsShow
← C
Lesson 169 of 1,2193. Operators and ExpressionsFree lesson

Logical OR `||`

Logical OR ||

What you'll learn: How to use the logical OR operator to check if at least one condition in your code is true.

The Power of "Either-Or"

In programming, we often face situations where we don't need every condition to be perfect—we just need one of them to work out. Think of it like a "Weekend Rule": you are happy if it is Saturday OR if it is Sunday. As long as one of those is true, your weekend plans are on!

In C, we represent this "either-or" logic using the double pipe symbol: ||.

How it Works

The || operator sits between two expressions. It looks at the left side and the right side and follows one simple rule: If at least one side is true, the whole thing is true.

The only time a logical OR expression results in "false" (zero) is if every single piece of it is false.

Imagine you are writing a program for a smart home. You want a heater to turn on if the temperature is too low OR if a human manually presses the "Override" button. Even if it is 80 degrees inside, the heater will turn on because the button was pressed.

Seeing it in Code

Here is how you might use || in a basic C program:

#include <stdio.h>

int main() {
    int isCloudy = 1;      // 1 means true
    int isRaining = 0;     // 0 means false
    
    // We stay inside if it is cloudy OR if it is raining
    if (isCloudy || isRaining) {
        printf("Better stay inside and learn C!\n");
    } else {
        printf("Let's go for a walk.");
    }

    return 0;
}

In the example above, even though it isn't raining (0), the condition passes because it is cloudy (1). The || operator is satisfied with that single "true" value.

Efficiency: Short-Circuiting

C is a very efficient language. When it uses the || operator, it reads from left to right. If the first condition is true, C doesn't even bother checking the second one. Why? Because the outcome is already decided! This is called "short-circuiting," and it helps your programs run just a little bit faster.

Key takeaway

The logical OR || operator returns true if at least one of its conditions is true.