Start learning free

LESSON 76 OF 347 · CONDITIONAL STATEMENTS

Or Operator in C#: Using || in Conditions

Lesson Read the full explanation

Sometimes we will need to run some code if at least one of two conditions is true. The logical or operator (||) returns true if either of the conditions is true. Otherwise, if both conditions are false, it returns false.
For example:

7 >= 10 || 10 < 12 // Evaluates to true

We have two conditions separated by || operator:

  • 7 >= 10, which evaluates to false
  • 10 < 12, which evaluates to true
As at least one of the conditions evaluates to true, the entire line will evaluate to true

Another example:

10 < 15 || 10 > 9 // Evaluates to true as both conditions are true

An example inside an if statement:

int x = 10;
if(x != 10 || 12 < x) { // Evaluates to false
    Console.WriteLine("This is true!");
}

Both conditions evalute to false, so the entire condition evaluates to false and the program prints nothing.

Assignment
Follow the Coding Tutorial and play with the or operator.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS

    Or Operator in C#, explained

    What the || operator does

    The logical or operator, written ||, joins two boolean expressions. The result is true when at least one side is true, and false only when both sides are false. Use it when one outcome should happen for several different reasons.

    LeftRightLeft || Right
    truetruetrue
    truefalsetrue
    falsetruetrue
    falsefalsefalse

    Each side is a full condition, usually a comparison. In 7 >= 10 || 10 < 12, the left comparison is false and the right one is true, so the whole expression is true.

    Using || in an if statement

    The result of || is a bool, so it fits directly inside an if condition:

    int x = 10;
    if (x != 10 || 12 < x) {
        Console.WriteLine("This is true!");
    }
    

    Here x != 10 is false and 12 < x is also false. With both sides false, the condition is false and the program prints nothing.

    A side effect worth knowing: || short-circuits. It evaluates the left side first, and if that is already true, the right side is never evaluated, because the answer can't change. This matters when the right side calls a method or could fail:

    bool a = true || SomeCheck();   // SomeCheck() is never called
    

    C# also has a single | that works on booleans. It always evaluates both sides. For conditions, use ||.

    Chaining several conditions

    You can join more than two conditions. The expression is true as soon as any one of them is true:

    int day = 6;
    bool weekend = day == 6 || day == 7;
    Console.WriteLine(weekend);   // True
    

    Each day == ... is a separate comparison, so day has to be repeated. Comparing against many values this way gets long quickly, but for two or three values it is the clearest form. The comparisons run from left to right, and the evaluation stops at the first one that is true.

    The result can also be stored: bool canEnter = isMember || hasTicket; keeps the outcome in a variable, which you can then test with if (canEnter) or flip with !canEnter.

    Common mistakes with ||

    • Writing a condition on only one side. Each side of || must be a complete boolean expression. x == 3 || 5 doesn't compile. Write x == 3 || x == 5.
    • Using one | by accident. On numbers, | is a bitwise operator, so a typo can change the meaning of the code instead of just failing to compile.
    • Mixing up "or" with plain English. "If the weather isn't sunny or the temperature is at most 85" is two comparisons joined by ||. Writing it as one sentence-like expression doesn't work in C#.
    • Using || when you mean &&. A check like x != 3 || x != 5 is true for every number, because no number equals both 3 and 5. To reject both values, use x != 3 && x != 5.
    • Comparing strings. In C#, == and != on two string values compare their text, so weather != "sunny" does what it looks like. Case matters: "Sunny" is not equal to "sunny".

    The exercise, step by step

    The code starts with two variables: weather is "sunny" and temperature is 90. The exercise has two steps.

    1. Write the if with ||. The first condition is that weather is not "sunny". The second is that temperature is at most 85. Join them with || and print "Learning to code" when either holds.
    2. Add the else. Write } else { on the closing line of the if and print "Going to the beach". The else needs no condition. It runs when the if condition is false, and for an || that means both sides are false.
    string weather = "sunny";
    int temperature = 90;
    
    if (weather != "sunny" || temperature <= 85) {
        Console.WriteLine("Learning to code");
    } else {
        Console.WriteLine("Going to the beach");
    }
    

    With the starting values, weather != "sunny" is false and temperature <= 85 is also false (90 is above 85). Both sides are false, so the else branch runs and the output is:

    Going to the beach
    

    Change weather to "rainy" or temperature to 80 and the first branch runs instead, even though only one side became true.

    Key takeaways

    • a || b is true when at least one of the two is true, and false only when both are false.
    • Each side of || must be a complete boolean expression.
    • || skips the right side when the left side is already true.
    • An else after an || condition runs only when both sides are false.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free