DEV Community

Sabin Sim
Sabin Sim

Posted on

05. C# (AND / OR Logical Operators & Short-Circuit)

0️⃣ Goal of This Lesson

When conditions become “multiple”,
how does the code decide what is true or false?

After this lesson, you should be able to explain:

  • why an if condition executes (or doesn’t)
  • which condition is evaluated first
  • why evaluation order actually matters

1️⃣ AND (&&) Operator — “All conditions must be true”

Meaning (in plain words)

A is true AND B is true → result is true


Run this example

using System;

class Program
{
    static void Main()
    {
        int number = 7;

        bool isBetween4And9 = number > 4 && number < 9;

        Console.WriteLine(isBetween4And9);
    }
}
Enter fullscreen mode Exit fullscreen mode

Evaluation

  • 7 > 4 → true
  • 7 < 9 → true
  • true && true → true

What if one condition fails?

int number = 10;
bool result = number > 4 && number < 9;
Enter fullscreen mode Exit fullscreen mode
  • left: 10 > 4 → true
  • right: 10 < 9 → false 👉 result: false

📌 AND requires every condition to be true


2️⃣ OR (||) Operator — “At least one must be true”

Meaning (in plain words)

A OR B → if either is true, the result is true


Example

int number = 7;

bool isTwoOrGreaterThanSix = number == 2 || number > 6;

Console.WriteLine(isTwoOrGreaterThanSix);
Enter fullscreen mode Exit fullscreen mode

Evaluation

  • 7 == 2 → false
  • 7 > 6 → true 👉 false || true → true

📌 OR succeeds if just one condition is true


3️⃣ You Can Chain Multiple Conditions

int number = 3;

bool result =
    number == 1
    || number == 2
    || number == 3;

Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

Key points

  • Line breaks do not affect logic
  • They exist only for readability

👉 C# does not care about line breaks
(as long as tokens are not split incorrectly)


4️⃣ Combining AND + OR (Very Important)

A common real-world rule:

“The number is 123
OR
it is even AND smaller than 20”


Code example

int number = 18;

bool result =
    number == 123
    || (number % 2 == 0 && number < 20);

Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

Why use parentheses?

  • Not to memorize precedence rules ❌
  • To clearly express intent ⭕

📌 When conditions grow, parentheses are not optional


5️⃣ Short-Circuit Evaluation — The Core Concept

Short-circuit with OR

int number = 10;

bool result =
    number > 5
    || ExpensiveCalculation();
Enter fullscreen mode Exit fullscreen mode

What happens?

  • number > 5 → true
  • OR needs only one true
  • 👉 ExpensiveCalculation() is never executed

📌 This behavior is called short-circuit evaluation


Short-circuit with AND

bool result =
    number < 5
    && ExpensiveCalculation();
Enter fullscreen mode Exit fullscreen mode
  • left side is false
  • AND must be false
  • 👉 right side is not evaluated

6️⃣ Why This Matters in Real Code

Practical rule (translated to real-world thinking):

Put cheap checks on the left,
expensive or risky checks on the right

Example

if (user != null && user.IsAdmin())
{
    // safe
}
Enter fullscreen mode Exit fullscreen mode
  • null check first
  • method call second

👉 Reversing the order can cause runtime errors


7️⃣ Naming Boolean Variables (Very Important)

Notice the pattern:

bool isEven;
bool isSmallerThan20;
bool isValid;
Enter fullscreen mode Exit fullscreen mode

Rule of thumb

Boolean variables should sound like questions

Why?

if (isValid)
Enter fullscreen mode Exit fullscreen mode

reads naturally as:

“If this is valid…”

📌 Common prefixes:

  • is...
  • has...
  • can...
  • should...

8️⃣ The Correct Mental Model

❌ Don’t think yet

  • “Too many conditions”
  • “Should I memorize this?”

✅ Correct focus

One condition → one bool
Multiple conditions → combine with AND / OR


🔑 One-Line Summary

AND means “all”,
OR means “at least one”,
and C# never evaluates what it doesn’t need to.

Top comments (0)