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.
| Left | Right | Left || Right |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
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 || 5doesn't compile. Writex == 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 likex != 3 || x != 5is true for every number, because no number equals both 3 and 5. To reject both values, usex != 3 && x != 5. - Comparing strings. In C#,
==and!=on twostringvalues compare their text, soweather != "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.
- Write the
ifwith||. The first condition is thatweatheris not"sunny". The second is thattemperatureis at most85. Join them with||and print"Learning to code"when either holds. - Add the
else. Write} else {on the closing line of theifand print"Going to the beach". Theelseneeds no condition. It runs when theifcondition 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 || bistruewhen at least one of the two istrue, andfalseonly when both arefalse.- Each side of
||must be a complete boolean expression. ||skips the right side when the left side is alreadytrue.- An
elseafter an||condition runs only when both sides are false.