➕ C# Relational Operators – Compare Values and Control Logic
🧲 Introduction – Why Use Relational Operators in C#?
Relational operators are used to compare two values and determine the relationship between them — such as equality, inequality, or relative size. They return a bool result (true or false), making them essential in conditional statements, loops, and logical decision-making.
🎯 In this guide, you’ll learn:
- The complete list of relational operators in C#
- Syntax and examples for each
- Common use cases in if,while, andforstatements
- Tips and best practices to avoid logic errors
🔍 Core Concept – What Are Relational Operators?
Relational operators (also called comparison operators) evaluate two operands and return a boolean result indicating their relationship.
📚 List of Relational Operators
| Operator | Symbol | Description | Example | Result | 
|---|---|---|---|---|
| Equal to | == | Checks if equal | 5 == 5 | true | 
| Not equal | != | Checks if not equal | 5 != 3 | true | 
| Greater than | > | Checks if left > right | 7 > 3 | true | 
| Less than | < | Checks if left < right | 2 < 5 | true | 
| Greater or equal | >= | Checks left ≥ right | 5 >= 5 | true | 
| Less or equal | <= | Checks left ≤ right | 4 <= 6 | true | 
💻 Code Example – Using Relational Operators
using System;
class RelationalDemo
{
    static void Main()
    {
        int a = 10, b = 5;
        Console.WriteLine($"a == b: {a == b}");
        Console.WriteLine($"a != b: {a != b}");
        Console.WriteLine($"a > b: {a > b}");
        Console.WriteLine($"a < b: {a < b}");
        Console.WriteLine($"a >= b: {a >= b}");
        Console.WriteLine($"a <= b: {a <= b}");
    }
}
📤 Output:
a == b: False  
a != b: True  
a > b: True  
a < b: False  
a >= b: True  
a <= b: False
🧠 Use Case – Conditional Logic
int age = 18;
if (age >= 18)
{
    Console.WriteLine("You are eligible to vote.");
}
else
{
    Console.WriteLine("You are not eligible.");
}
✅ Commonly used in:
- if/elseconditions
- while,- forloop limits
- Input validation
- Filtering with iforswitchstatements
💡 Tips, Pitfalls & Best Practices
💡 Tip: Always double-check the operands’ types. Mixed-type comparisons can cause unexpected results.
⚠️ Pitfall: Don’t confuse = (assignment) with == (comparison).
📘 Best Practice: Use parentheses in complex conditions for clarity:
if ((score >= 50) && (attempts < 3)) { ... }
📊 Boolean Results of Relational Expressions
| Expression | Value | 
|---|---|
| 10 == 10 | true | 
| 20 != 15 | true | 
| 7 < 5 | false | 
| 3 >= 3 | true | 
📌 Summary – Recap & Next Steps
Relational operators are essential for control flow in C#. They help your program make decisions based on comparisons and user input.
🔍 Key Takeaways:
- Use relational operators to compare two values
- They return trueorfalseand guide your program’s logic
- Commonly used in conditions, loops, and validations
⚙️ Next up: Explore ➕ C# Logical Operators for combining multiple conditions with AND, OR, and NOT.
❓ FAQ – C# Relational Operators
❓ What does == do in C#?
✅ It checks whether two operands are equal.
❓ What is the difference between != and ==?
✅ == checks for equality, while != checks for inequality.
❓ Can I use relational operators with strings?
✅ Yes, but comparisons are case-sensitive and based on Unicode values.
❓ What happens if I use = instead of ==?
❌ This assigns a value instead of comparing, which can lead to logic errors.
❓ Are relational operators used in loops?
✅ Yes. They define loop continuation or exit conditions.
Share Now :
