Introduction
Logical operators are used to perform logical operations on Boolean expressions. They are commonly used in decision-making and loops to combine multiple conditions. In C#, the result of a logical operation is a Boolean value (true
or false
).
List of Logical Operators
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)
1. Logical AND (&&)
The logical AND operator returns true
if both operands are true
. If either operand is false
, it returns false
.
Syntax
result = operand1 && operand2;
Example
bool a = true;
bool b = false;
bool result = a && b;
Console.WriteLine("Result: " + result);
Output:
Result: False
Example with Multiple Conditions
int age = 25;
bool hasLicense = true;
bool canDrive = (age >= 18) && hasLicense;
Console.WriteLine("Can Drive: " + canDrive);
Output:
Can Drive: True
2. Logical OR (||)
The logical OR operator returns true
if at least one of the operands is true
. If both operands are false
, it returns false
.
Syntax
result = operand1 || operand2;
Example
bool a = true;
bool b = false;
bool result = a || b;
Console.WriteLine("Result: " + result);
Output:
Result: True
Example with Multiple Conditions
int age = 16;
bool hasPermission = true;
bool canAttendParty = (age >= 18) || hasPermission;
Console.WriteLine("Can Attend Party: " + canAttendParty);
Output:
Can Attend Party: True
3. Logical NOT (!)
The logical NOT operator inverts the value of its operand. If the operand is true
, it returns false
; if the operand is false
, it returns true
.
Syntax
result = !operand;
Example
bool isRaining = true;
bool isSunny = !isRaining;
Console.WriteLine("Is Sunny: " + isSunny);
Output:
Is Sunny: False
Example with Conditional Statements
bool isWeekend = false;
if (!isWeekend)
{
Console.WriteLine("It's a weekday.");
}
else
{
Console.WriteLine("It's the weekend.");
}
Output:
It's a weekday.
Combining Logical Operators
Logical operators can be combined to form complex logical expressions. Parentheses can be used to control the order of operations.
Example: Combining Logical AND and OR
int age = 20;
bool hasLicense = true;
bool hasInsurance = false;
bool canDrive = (age >= 18) && hasLicense && hasInsurance;
Console.WriteLine("Can Drive: " + canDrive);
Output:
Can Drive: False
Example: Using Logical Operators in Conditional Statements
Logical operators are commonly used in if
statements to control the flow of a program based on multiple conditions.
int age = 17;
bool hasParentalConsent = true;
if (age >= 18 || hasParentalConsent)
{
Console.WriteLine("You can watch the movie.");
}
else
{
Console.WriteLine("You cannot watch the movie.");
}
Output:
You can watch the movie.
Short-Circuit Evaluation
C# logical operators use short-circuit evaluation, meaning the second operand is evaluated only if needed.
Example: Short-Circuit AND (&&)
bool result = false && SomeMethod(); // SomeMethod is not called because the first operand is false
Example: Short-Circuit OR (||)
bool result = true || SomeMethod(); // SomeMethod is not called because the first operand is true
Example Function
bool SomeMethod()
{
Console.WriteLine("SomeMethod is called.");
return true;
}
Conclusion
Logical operators are essential for making decisions and controlling the flow of your C# programs. By understanding and using logical AND, OR, and NOT operators, you can combine multiple conditions and build complex logical expressions. This allows you to create more dynamic and flexible programs, capable of handling a variety of scenarios.