<aside> Comparison operators and logical operations are essential for creating conditional logic in C# programs. They form the foundation of decision-making in your code.
</aside>
Comparison operators evaluate relationships between values and return boolean (true/false) results.
| Operator | Name | Example | Result |
|---|---|---|---|
| < | Less than | 5 < 10 | true |
| <= | Less than or equal to | 5 <= 5 | true |
| > | Greater than | 10 > 5 | true |
| >= | Greater than or equal to | 10 >= 11 | false |
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 10 | true |
Mathematical operators perform arithmetic operations on numeric values.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division | 10 / 2 | 5 |
| % | Modulus (Remainder) | 10 % 3 | 1 |
| ++ | Increment | int x = 5; x++; | 6 |
| -- | Decrement | int x = 5; x--; | 4 |
| += | Addition assignment | int x = 5; x += 3; | 8 |
| -= | Subtraction assignment | int x = 5; x -= 3; | 2 |
| *= | Multiplication assignment | int x = 5; x *= 3; | 15 |
| /= | Division assignment | int x = 10; x /= 2; | 5 |
| %= | Modulus assignment | int x = 10; x %= 3; | 1 |
// Examples of mathematical operators
int a = 10;
int b = 3;
int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3 (integer division)
int remainder = a % b; // 1
// Increment and decrement
int c = 5;
c++; // c is now 6
c--; // c is back to 5
// Compound assignment
int d = 10;
d += 5; // d is now 15 (same as d = d + 5)
d -= 3; // d is now 12 (same as d = d - 3)
d *= 2; // d is now 24 (same as d = d * 2)
d /= 6; // d is now 4 (same as d = d / 6)
d %= 3; // d is now 1 (same as d = d % 3)
Note that when dividing integers, the result is an integer (decimal part is truncated). For floating-point division, at least one operand must be a floating-point type:
int x = 10;
int y = 3;
double result1 = x / y; // 3.0 (integer division occurs before conversion)
double result2 = x / (double)y; // 3.333... (proper floating-point division)
| Operator | Name | Description |
|---|---|---|
| && | AND | Both conditions must be true |
| ! | NOT | Inverts the boolean value |
// Basic comparison
int age = 25;
bool isAdult = age >= 18; // true
// Combining conditions with AND
bool hasLicense = true;
bool canDrive = isAdult && hasLicense; // true
// Using OR operator
bool isHoliday = false;
bool isWeekend = true;
bool canRest = isHoliday || isWeekend; // true
// Using NOT operator
bool isWorking = !isHoliday; // true
// Age verification
if (age >= 18 && hasLicense)
{
Console.WriteLine("Can drive");
}
// Temperature check
int temperature = 25;
if (temperature < 0 || temperature > 30)
{
Console.WriteLine("Extreme temperature!");
}
// Password validation
string password = "secret123";
bool isValid = password.Length >= 8 && password.Contains("123");
<aside> 💡 Best Practices: • Use parentheses to make complex conditions more readable • Combine operators carefully to avoid logical errors • Consider the order of evaluation in complex conditions • Use meaningful variable names for boolean values
</aside>
<aside> ⚠️ Common Pitfalls: • Confusing == (comparison) with = (assignment) • Not considering all possible conditions • Over-complicating logical expressions • Forgetting that && evaluates before ||
</aside>
C# uses short-circuit evaluation for logical operators: