Course contentsShow
← C
Lesson 248 of 1,2194. Control FlowFree lesson

Logical OR (||) for combined conditions

Logical OR (||) for combined conditions

What you'll learn: How to use the Logical OR operator to execute code if at least one of multiple conditions is true.

The "Either-Or" Logic

In programming, you often face situations where you want to trigger an action if any one of several requirements is met. Think of it like a discount at a movie theater: you get a cheaper ticket if you are a student OR if you are a senior citizen. You don't need to be both; meeting just one condition is enough to get the deal.

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

How it Works

The || operator sits between two expressions. If the expression on the left is true, the whole thing is true. If the expression on the right is true, the whole thing is true. The only way the entire statement results in "false" is if both sides are false.

Here is how you might see it in a real program:

#include <stdio.h>

int main() {
    int day = 6; // Let's say 6 is Saturday and 7 is Sunday

    // Check if today is a weekend day
    if (day == 6 || day == 7) {
        printf("Relax! It's the weekend.\n");
    } else {
        printf("Back to work.\n");
    }

    return 0;
}

In this example, the computer first checks if day is 6. Since that is true, it doesn't even bother checking if day is 7. It simply enters the block and prints the message.

Short-Circuiting

C is efficient. When using ||, it uses something called "short-circuit evaluation." If the first condition is true, C already knows the entire if statement must be true. It "short-circuits" the process and skips evaluating the second half entirely. This saves processing time and can even prevent errors if the second condition is something complex.

Why use it?

Without the || operator, you would have to write two separate if statements or a messy set of nested branches to achieve the same result. The Logical OR keeps your code clean, readable, and closer to how we naturally speak and think.

Key takeaway

The || operator returns true if at least one side is true, allowing your program to handle multiple valid paths with a single check.