C# > Core C# > Operators and Expressions > Logical Operators
Logical AND and OR Operators
This snippet demonstrates the use of the logical AND (&&
) and logical OR (||
) operators in C#. These operators are fundamental for controlling program flow based on multiple conditions.
Code Demonstration
The code showcases the &&
(AND) and ||
(OR) operators.
The first if
statement uses &&
to check if it's raining AND if the person has an umbrella. Both conditions must be true for the code inside the if
block to execute.
The second if
statement uses ||
to check if it's the weekend OR if the person has money. At least one of these conditions needs to be true for the if
block to execute.
The third if
statement demonstrates the combining logical operators. The condition (age >= 18 && hasLicense) || hasInsurance
checks if either (the person is 18 or older AND has a license) OR the person has insurance.
using System;
public class LogicalOperators
{
public static void Main(string[] args)
{
bool isRaining = true;
bool hasUmbrella = false;
// Logical AND (&&): Both conditions must be true
if (isRaining && hasUmbrella)
{
Console.WriteLine("You can go outside without getting wet.");
}
else
{
Console.WriteLine("You might get wet if you go outside.");
}
// Logical OR (||): At least one condition must be true
bool isWeekend = true;
bool hasMoney = false;
if (isWeekend || hasMoney)
{
Console.WriteLine("You can enjoy your time.");
}
else
{
Console.WriteLine("It might be a boring day.");
}
// Combining AND and OR
int age = 25;
bool hasLicense = true;
bool hasInsurance = true;
if ((age >= 18 && hasLicense) || hasInsurance)
{
Console.WriteLine("You are eligible to drive (with some restrictions).");
}
else
{
Console.WriteLine("You are not eligible to drive.");
}
}
}
Concepts Behind the Snippet
Logical operators evaluate boolean expressions. &&
returns true
only if both operands are true
. ||
returns true
if at least one operand is true
. C# also features short-circuiting, meaning that the second operand of &&
will only be evaluated if the first operand is true
, and the second operand of ||
will only be evaluated if the first operand is false
. This can improve performance by avoiding unnecessary evaluations.
Real-Life Use Case
Imagine a website that requires users to be both logged in AND have sufficient permissions to access a particular resource. Logical AND would be used to ensure both conditions are met. Conversely, a user might be granted access if they have admin privileges OR if they belong to a specific group; logical OR is ideal in such scenarios.
Best Practices
Interview Tip
Be prepared to explain the concept of short-circuiting in C# logical operators. You might be asked to trace code execution involving complex boolean expressions.
When to use them
Use logical operators when you need to combine multiple boolean conditions to make a decision in your code. This is common in control flow statements like if
, else if
, and while
.
Alternatives
While logical operators are fundamental, complex conditional logic can sometimes be simplified using techniques like:
Pros
Cons
FAQ
-
What is short-circuiting in logical operators?
Short-circuiting means that the second operand of&&
will not be evaluated if the first operand isfalse
, and the second operand of||
will not be evaluated if the first operand istrue
. This can improve performance. -
What is the difference between
&
and&&
?
&
is the bitwise AND operator, while&&
is the logical AND operator.&&
performs short-circuiting, while&
always evaluates both operands.