Welcome to The Coding College! In this tutorial, we will dive into C# Logical Operators, which are essential for combining conditions and performing logical operations. These operators allow programmers to execute multiple conditions at once and are widely used in decision-making statements.
Explore more coding tutorials at thecodingcollege.com, your trusted resource for learning programming efficiently.
What are Logical Operators in C#?
Logical operators in C# are used to combine two or more Boolean expressions (conditions) and return a Boolean value:
true
if the condition(s) are satisfiedfalse
otherwise
They are primarily used in conditional statements, loops, and logical comparisons.
List of C# Logical Operators
Operator | Name | Description | Example | Output |
---|---|---|---|---|
&& | Logical AND | Returns true if both conditions are true | (x > 5) && (y < 10) | true |
` | ` | Logical OR | Returns true if at least one condition is true | |
! | Logical NOT | Reverses the result: true becomes false , and vice versa | !(x > 5) | false |
1. Logical AND Operator (&&
)
The &&
operator checks if both conditions are true
. If any condition is false
, the result will be false
.
Example:
int x = 10;
int y = 5;
if (x > 5 && y < 10)
{
Console.WriteLine("Both conditions are true.");
}
else
{
Console.WriteLine("At least one condition is false.");
}
// Output: Both conditions are true.
2. Logical OR Operator (||
)
The ||
operator checks if at least one condition is true
. If both conditions are false
, the result will be false
.
Example:
int x = 10;
int y = 15;
if (x > 5 || y < 10)
{
Console.WriteLine("At least one condition is true.");
}
else
{
Console.WriteLine("Both conditions are false.");
}
// Output: At least one condition is true.
3. Logical NOT Operator (!
)
The !
operator reverses the result of a Boolean expression:
true
becomesfalse
false
becomestrue
Example:
int x = 10;
if (!(x < 5))
{
Console.WriteLine("x is not less than 5.");
}
else
{
Console.WriteLine("x is less than 5.");
}
// Output: x is not less than 5.
Combining Logical Operators
You can combine multiple logical operators to form complex conditions. Parentheses ()
can be used to group conditions for better readability and precedence.
Example:
int a = 10;
int b = 20;
int c = 30;
if ((a > 5 && b < 25) || c == 30)
{
Console.WriteLine("The condition is true.");
}
else
{
Console.WriteLine("The condition is false.");
}
// Output: The condition is true.
Logical Operator Precedence
When multiple logical operators are used, their precedence determines the order of execution:
!
(Logical NOT)&&
(Logical AND)||
(Logical OR)
To ensure clarity, always use parentheses to control the order explicitly.
Example Without Parentheses:
int x = 10;
int y = 5;
if (x > 5 || y < 10 && y > 2)
{
Console.WriteLine("Condition is true.");
}
// Output: Condition is true.
Example With Parentheses:
if ((x > 5 || y < 10) && y > 2)
{
Console.WriteLine("Condition is true.");
}
Using parentheses improves readability and ensures you get the expected results.
Practical Use Case: Logical Operators
Logical operators are crucial in programming for decision-making. Here’s an example:
Voting Eligibility Check:
int age = 20;
bool hasVoterID = true;
if (age >= 18 && hasVoterID)
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
// Output: You are eligible to vote.
Benefits of Using Logical Operators
- Efficient Condition Handling: Logical operators allow you to handle multiple conditions in a single statement.
- Simplified Code: Reduces the need for nested
if
statements. - Enhanced Readability: Grouping conditions with
&&
,||
, and!
improves code clarity.
Practice Exercise
Try solving this problem:
int marks = 85;
bool passedExam = true;
if (marks > 80 && passedExam)
{
Console.WriteLine("Congratulations! You passed with distinction.");
}
else if (marks >= 50 || passedExam)
{
Console.WriteLine("You passed the exam.");
}
else
{
Console.WriteLine("You failed the exam.");
}
Question:
- What will be the output when
marks = 85
andpassedExam = true
?
Conclusion
C# Logical Operators are powerful tools that allow you to evaluate and combine multiple conditions efficiently. By mastering these operators, you can write cleaner, more logical, and optimized code.
For more tutorials and coding tips, visit thecodingcollege.com and take your programming skills to the next level.