Home » C# Logical Operators : A Tutorial

C# Logical Operators : A Tutorial

by wskandot71

Logical operators in C# are used to perform Boolean (true/false) logic operations. They are commonly used in conditional statements and loops to evaluate multiple conditions.

This tutorial will cover:

  • List of Logical Operators in C#
  • Using Logical Operators with Examples
  • Operator Precedence in Logical Expressions
  • Short-Circuit Evaluation in Logical Operators
  • Best Practices for Using Logical Operators

1. List of Logical Operators in C#

C# provides the following logical operators, which work with Boolean values (true or false).

Operator Description Example
&& Logical AND a && b
` `
! Logical NOT !a

2. Using Logical Operators with Examples

Logical AND (&&)

The && (AND) operator returns true only if both operands are true.

bool isAdult = true;
bool hasID = true;

if (isAdult && hasID)
{
    Console.WriteLine("Entry allowed.");
}
else
{
    Console.WriteLine("Entry denied.");
}
  • If both isAdult and hasID are true, the output is “Entry allowed.”.
  • If either condition is false, the output is “Entry denied.”.

Logical OR (||)

The || (OR) operator returns true if at least one operand is true.

bool hasCoupon = false;
bool isVIP = true;

if (hasCoupon || isVIP)
{
    Console.WriteLine("Discount applied.");
}
else
{
    Console.WriteLine("No discount.");
}
  • If either hasCoupon or isVIP is true, the output is “Discount applied.”.

Logical NOT (!)

The ! (NOT) operator reverses the Boolean value.

bool isRaining = false;

if (!isRaining)
{
    Console.WriteLine("Go outside.");
}
else
{
    Console.WriteLine("Stay inside.");
}
  • Since isRaining is false, !isRaining is true, so “Go outside.” is printed.

3. Operator Precedence in Logical Expressions

Logical operators follow precedence rules, meaning some operators are evaluated before others.

Operator Precedence Table

Operator Description Precedence
! Logical NOT Highest
&& Logical AND Medium
` `

Example of Operator Precedence

bool result = true || false && false;
Console.WriteLine(result); // Output: true
  • false && false is evaluated first (false).
  • true || false results in true.

Using Parentheses for Clarity

bool result = (true || false) && false;
Console.WriteLine(result); // Output: false
  • Parentheses ensure true || false is evaluated first.

4. Short-Circuit Evaluation in Logical Operators

C# uses short-circuit evaluation, meaning:

  • In &&, if the first condition is false, the second condition is not evaluated.
  • In ||, if the first condition is true, the second condition is not evaluated.

Short-Circuit in && (Logical AND)

bool CheckCondition1()
{
    Console.WriteLine("Checking condition 1...");
    return false;
}

bool CheckCondition2()
{
    Console.WriteLine("Checking condition 2...");
    return true;
}

if (CheckCondition1() && CheckCondition2())
{
    Console.WriteLine("Both conditions are true.");
}
else
{
    Console.WriteLine("At least one condition is false.");
}

Output:

Checking condition 1...
At least one condition is false.
  • CheckCondition2() is not called because CheckCondition1() returns false.

Short-Circuit in || (Logical OR)

if (CheckCondition1() || CheckCondition2())
{
    Console.WriteLine("At least one condition is true.");
}

Output:

Checking condition 1...
Checking condition 2...
At least one condition is true.
  • CheckCondition2() is called because CheckCondition1() is false.

5. Combining Logical Operators

You can combine logical operators to create complex conditions.

Example: Complex Condition

bool isWeekend = true;
bool hasMoney = false;
bool isHoliday = true;

if ((isWeekend || isHoliday) && hasMoney)
{
    Console.WriteLine("Go on a trip.");
}
else
{
    Console.WriteLine("Stay at home.");
}
  • Since hasMoney is false, the final result is “Stay at home.”.

6. Logical Operators with Conditional Statements

Logical operators are commonly used in if, while, and do-while loops.

Example: Logical Operators in a while Loop

int number = 1;

while (number < 10 && number % 2 == 0)
{
    Console.WriteLine("Number is even and less than 10: " + number);
    number++;
}

7. Best Practices for Using Logical Operators

Use Short-Circuit Evaluation to Improve Performance

if (x != 0 && y / x > 2) // Prevents division by zero

Use Parentheses for Readability

if ((isWeekend || isHoliday) && hasMoney)

Avoid Redundant Conditions

if (isSunny == true) // Redundant
if (isSunny) // Better

Use ! Carefully for Boolean Comparisons

if (!isLoggedIn) // Clearer than isLoggedIn == false

Check for Null Before Using Logical Operators

if (user != null && user.IsActive)

Conclusion

  • Logical operators (&&, ||, !) are used for Boolean logic.
  • Short-circuit evaluation improves performance.
  • Operator precedence determines evaluation order.
  • Parentheses improve readability and avoid ambiguity.
  • Logical operators are essential for conditional statements and loops.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.