1. Introduction
Let's start with a real-life analogy. Imagine a recipe:
Mix flour, eggs and sugar whisk add butter whisk again.
Looks weird, right? Now imagine there's a comma or parentheses between "whisk" and "add". The order of actions gets way clearer.
Programming is kinda the same when you write a complex expression:
bool result = a > 0 && b < 10 || c == 3;
What's the order here? What gets calculated first: a > 0 && b < 10, then || c == 3? Or the other way around? Without a clear calculation order, the computer can "mix up the ingredients" and your result will be totally unexpected.
2. Operator Precedence in C#
C# (just like most programming languages) uses a specific operator precedence table.
Operators with higher precedence get executed before operators with lower precedence.
Check out the most important part of this table (for conditional expressions):
| Operator | Description | Precedence |
|---|---|---|
| () | Parentheses | highest |
| ! | Logical "NOT" | high |
| ==, != | Equals / not equals | medium |
| <, >, <=, >= | Comparisons | medium |
| && | Logical "AND" | lower |
| || | Logical "OR" | even lower |
(There are also arithmetic operators—they have higher precedence than logical ones.)
Main Rule
"AND" operators (&&) have higher precedence than "OR" (||),
so "AND" gets calculated first, then "OR".
Flowchart for Condition Precedence Calculation
In practice: a > 0 && b < 10 || c == 3 — first a > 0 && b < 10 is calculated, then the result is compared with c == 3 using ||.
3. Operator Direction: Left to Right and Vice Versa
Besides precedence, there's another word: associativity.
This is the rule for which direction operators are calculated if they're "next to each other" and have the same precedence.
Example:
bool a = true, b = false, c = true;
bool result = a && b && c;
Question: how does this get calculated?
Answer: && is left to right associative.
So first a && b, then the result with c.
Applied to conditions:
| Operator | Associativity |
|---|---|
| &&, || | left to right |
| ! | right to left |
4. Parentheses—Your Best Friend in Complex Conditions
This is where the magic (and the rescue) happens. When you write an expression, parentheses change the precedence and make your code clearer and safer.
Example 1 (no parentheses):
// We want: "If the user is an adult and a citizen, or if they have a special pass"
bool isAdult = age >= 18;
bool isCitizen = country == "Belarus";
bool hasPermit = hasSpecialPass == true;
if (isAdult && isCitizen || hasPermit)
{
Console.WriteLine("Access granted!");
}
How C# calculates:
First it calculates isAdult && isCitizen, then compares the result with hasPermit using ||.
- Let's say, isAdult = true, isCitizen = false, hasPermit = true
isAdult && isCitizen→true && false→falsefalse || true→true
So, if a person has a special pass, they'll get in anyway.
And if they don't have a special pass, but they're an adult citizen—they'll get in too.
Example 2 (with parentheses):
if (isAdult && (isCitizen || hasPermit))
{
Console.WriteLine("Access granted!");
}
Now the program first checks:
isCitizen || hasPermit, and then that result together with isAdult.
So you need to be an adult and (either a citizen or have a special pass).
A tiny difference in parentheses—a huge difference in logic!
GO TO FULL VERSION