Comparison and logical operators — C#
What is this and why?
Imagine you're playing a computer game. To complete a level, you need to check different conditions: "If the hero has less than 10 lives, then activate the shield" or "If the player pressed the button AND collected all the coins, open the door".
In programming, such checks are done using comparison operators and logical operators. They help the program "think" and make decisions. Without them, the code would just be a list of commands that always execute the same way, but with them, the program can react to different situations.
For example, you're writing a grade calculator. If a student scored more than 90 points — "Excellent", if less than 50 — "Poor". Or you check if a user can log into the system: if the password is correct AND the account is not blocked — let them in.
Comparison operators compare numbers or text. Logical operators combine several conditions into one. Together, they are the foundation of any "smart" code.
Syntax and code examples
In C#, comparison operators look like this:
==— equal to (note: two equal signs, not one!)!=— not equal to>— greater than<— less than>=— greater than or equal to<=— less than or equal to
Logical operators:
&&— AND (both conditions must be true)||— OR (at least one condition is true)!— NOT (inverts true to false and vice versa)
The result of a comparison is always true or false.
Example 1: Age check
int age = 14;
bool canDrive = age >= 18; // false, because 14 is less than 18
if (canDrive)
{
Console.WriteLine("You can drive a car!");
}
else
{
Console.WriteLine("Grow up a bit more."); // This will execute
}
// Explanation:
// age >= 18 — this is a comparison. It gives false.
// if checks true/false. If true — executes the first block, if false — the second.
Example 2: Login and password check
string login = "user123";
string password = "qwerty";
// Check that login equals "admin" AND password equals "12345"
if (login == "admin" && password == "12345")
{
Console.WriteLine("Welcome, admin!");
}
else
{
Console.WriteLine("Invalid login or password.");
}
// Explanation:
// && means "AND". Both conditions must be true.
// login == "admin" — false (because login = "user123")
// So the whole condition is false — we go to else.
Example 3: Discount for students or pensioners
int age = 20;
bool isStudent = true;
// Discount if age is less than 18 OR the person is a student
if (age < 18 || isStudent)
{
Console.WriteLine("50% discount!");
}
else
{
Console.WriteLine("Full price.");
}
// Explanation:
// || means "OR". One true is enough.
// age < 18 — false (20 is not less than 18)
// isStudent — true
// So the condition is true — discount is given.
Example 4: Check that a number is not zero
int number = 7;
if (number != 0)
{
Console.WriteLine("Number is not zero, can divide.");
}
else
{
Console.WriteLine("Cannot divide by zero!");
}
// Explanation:
// != checks "not equal to".
// 7 != 0 — this is true, so the first block executes.
Example 5: Complex condition with NOT
bool isRaining = false;
bool hasUmbrella = true;
// If it is NOT raining OR has an umbrella — can go for a walk
if (!isRaining || hasUmbrella)
{
Console.WriteLine("Let's go for a walk!");
}
else
{
Console.WriteLine("Stay home.");
}
// Explanation:
// !isRaining — this is "NOT isRaining". isRaining = false, so !false = true.
// true || true = true — we go for a walk.
// If isRaining = true and hasUmbrella = false, then !true = false, false || false = false — we stay home.
Comparison with Python
In Python, the same operators look almost the same, but there are a couple of differences:
- Instead of
&&they writeand - Instead of
||they writeor - Instead of
!they writenot - Comparison operators are the same:
==,!=,>,<,>=,<=
Example in Python (the same as in C#):
age = 14
can_drive = age >= 18 # False
if can_drive:
print("You can drive a car!")
else:
print("Grow up a bit more.")
# Logical operators:
login = "user123"
password = "qwerty"
if login == "admin" and password == "12345":
print("Welcome, admin!")
else:
print("Invalid login or password.")
The main difference: in C#, logical operators are symbols (&&, ||, !), while in Python they are words (and, or, not). Otherwise, the logic is the same.
Typical beginner mistakes
-
Confusing = and ==
=— this is assignment (put a value into a variable).
==— this is comparison (check if values are equal).
Mistake:if (age = 18)— this is not allowed! You needif (age == 18). -
Forgetting operator precedence
Comparisons are performed first, then logical operators. But it's better to use parentheses for clarity.
Example:if (age > 18 && name == "Tom" || score > 100)— it's unclear what was meant. Correct:if ((age > 18 && name == "Tom") || score > 100). -
Confusing && and ||
&&requires that all