Relational operators compare two values and give you a bool result — true or false. They're the building blocks of decision-making in your programs. Every if-statement, loop, and condition depends on these.
The Comparison Operators
Here's every relational operator in C#:
int x = 10;
int y = 5;
Console.WriteLine(x == y); // False — equal to
Console.WriteLine(x != y); // True — not equal to
Console.WriteLine(x > y); // True — greater than
Console.WriteLine(x < y); // False — less than
Console.WriteLine(x >= y); // True — greater than or equal to
Console.WriteLine(x <= y); // False — less than or equal to
Notice the double equals == for comparison. A single = is assignment. Mixing them up is a classic mistake — the compiler will catch it though.
Comparing Different Types
You can compare values of different numeric types — C# handles the conversion automatically:
int score = 85;
double passing = 70.0;
Console.WriteLine(score >= passing); // True — int and double work fine together
For strings, == compares the actual text (not memory addresses like in some languages):
string name1 = "Alice";
string name2 = "Alice";
string name3 = "Bob";
Console.WriteLine(name1 == name2); // True — same text
Console.WriteLine(name1 == name3); // False — different text
Using Comparisons in Practice
Relational operators really shine inside conditions:
int age = 18;
if (age >= 18) {
Console.WriteLine("You can vote!");
}
if (age < 13) {
Console.WriteLine("You're a child");
} else if (age < 20) {
Console.WriteLine("You're a teenager");
} else {
Console.WriteLine("You're an adult");
}
You'll use these operators constantly once we get into control flow. For now, get comfortable with what each one checks.
Try it Yourself →