C# Logical Operators


Logical operators in C# are used to perform logical operations on boolean expressions. These operators are essential for controlling the flow of a program based on multiple conditions. Here’s a detailed explanation of the logical operators available in C#, along with examples for each.

1. Logical Operators Overview

OperatorDescriptionExample
&&Logical ANDa && b
``
!Logical NOT!a

2. Detailed Explanation

Logical AND (&&)

The logical AND operator returns true if both operands are true; otherwise, it returns false. It short-circuits, meaning if the first operand is false, the second operand is not evaluated.

Example:

bool a = true; bool b = false; bool result = a && b; // result = false // Short-circuiting example bool c = (5 > 3) && (10 < 5); // c = false, second condition is not evaluated

Logical OR (||)

The logical OR operator returns true if at least one of the operands is true; it returns false only if both operands are false. Like the AND operator, it also short-circuits; if the first operand is true, the second operand is not evaluated.

Example:

bool a = true; bool b = false; bool result = a || b; // result = true // Short-circuiting example bool c = (5 < 3) || (10 > 5); // c = true, second condition is not evaluated

Logical NOT (!)

The logical NOT operator negates the value of the operand. If the operand is true, it returns false, and if the operand is false, it returns true.

Example:

bool a = true; bool b = !a; // b = false bool c = false; bool d = !c; // d = true

3. Using Logical Operators in C#

Logical operators are commonly used in conditional statements to combine multiple conditions. Here's an example of how to use logical operators in an if statement:

using System; class Program { static void Main() { int age = 20; bool hasPermission = true; if (age >= 18 && hasPermission) { Console.WriteLine("You can vote."); } else { Console.WriteLine("You cannot vote."); } } }

4. Truth Tables

Here’s a truth table that summarizes the behavior of the logical operators:

| A | B | A && B | A || B | !A | |-------|-------|--------|--------|-------| | true | true | true | true | false | | true | false | false | true | false | | false | true | false | true | true | | false | false | false | false | true |

5. Operator Precedence

Logical operators have specific precedence rules. The NOT operator (!) has higher precedence than the AND operator (&&), which in turn has higher precedence than the OR operator (||). This means that NOT operations are evaluated first, followed by AND operations, and finally OR operations.

Example:

bool a = false; bool b = true; bool c = true; bool result = !a || (b && c); // result = true // Here, `!a` is evaluated first, then `b && c`, and finally `||`.

6. Practical Example

Here’s a complete example demonstrating the use of logical operators:

using System; class Program { static void Main() { bool isRaining = true; bool hasUmbrella = false; if (isRaining && !hasUmbrella) { Console.WriteLine("You should stay indoors."); } else if (isRaining && hasUmbrella) { Console.WriteLine("You can go outside with your umbrella."); } else { Console.WriteLine("It's a nice day to go outside!"); } } }

Summary

  • Logical operators in C# are used to combine or negate boolean expressions.
  • The three main logical operators are AND (&&), OR (||), and NOT (!).
  • Logical operators are commonly used in conditional statements and loops to evaluate multiple conditions.
  • Understanding how to use logical operators effectively is crucial for implementing complex logic and controlling the flow of your C# programs.