
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# - Relational Operators
What Are Relational Operators in C#?
C# relationship operators compare two operands and return a Boolean value (either true or false). These operators are useful for decision-making, loops, and data validation.
List of C# Relational Operators
Following table shows all the relational operators supported by C#. Assume variable A holds 10 and variable B holds 20, then −
Operator | Symbol | Description | Example |
---|---|---|---|
Equal to | == | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
Not equal to | != | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
Greater than | > | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
Less than | < | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
Greater than or equal to | >= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
Less than or equal to | <= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
Example of Relational Operators
The following example demonstrates all the relational operators available in C# −
using System; class Program { static void Main(string[] args) { int a = 21; int b = 10; if (a == b) { Console.WriteLine("Line 1 - a is equal to b"); } else { Console.WriteLine("Line 1 - a is not equal to b"); } if (a < b) { Console.WriteLine("Line 2 - a is less than b"); } else { Console.WriteLine("Line 2 - a is not less than b"); } if (a > b) { Console.WriteLine("Line 3 - a is greater than b"); } else { Console.WriteLine("Line 3 - a is not greater than b"); } /* Lets change value of a and b */ a = 5; b = 20; if (a <= b) { Console.WriteLine("Line 4 - a is either less than or equal to b"); } if (b >= a) { Console.WriteLine("Line 5-b is either greater than or equal to b"); } } }
When the above code is compiled and executed, it produces the following result −
Line 1 - a is not equal to b Line 2 - a is not less than b Line 3 - a is greater than b Line 4 - a is either less than or equal to b Line 5 - b is either greater than or equal to b
Relational Operators With Conditional Statements
You can use the relationship operators in conditional statements to make the comparison between two operands.
Example
In the following example, we are using relational operators to compare two integer values and print the results.
using System; class Program { 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)); } }
When the above code is compiled and executed, it produces the following result −
a == b: False a != b: True a > b: True a < b: False a >= b: True a <= b: False
Example
In the following example, we are using relational operators inside an if-else statement to determine the temperature condition.
using System; class Program { static void Main() { int temperature = 25; if (temperature > 30) { Console.WriteLine("It's a hot day!"); } else { Console.WriteLine("The weather is pleasant."); } } }
When the above code is compiled and executed, it produces the following result −
The weather is pleasant.
Relational Operators in a for Loop
You can use the relationship operators in the loops to control the execution of the loop.
Example
In the following example, we are using relational operators in a for loop to control the number of iterations.
using System; class Program { static void Main() { for (int i = 1; i <= 5; i++) { Console.WriteLine("Iteration: " + i); } } }
When the above code is compiled and executed, it produces the following result −
Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5
Use Cases
Here are some of the real-world use cases for relational operators:
Example: Validating User Age
In the following example, we are using relational operators to validate whether a user is eligible to vote.
using System; class Program { static void Main() { int age = 17; if (age >= 18) { Console.WriteLine("You are eligible to vote."); } else { Console.WriteLine("You are not eligible to vote."); } } }
When the above code is compiled and executed, it produces the following result −
You are not eligible to vote.
Example: Checking Account Balance for Withdrawal
In the following example, we are using relational operators to check if a withdrawal amount is valid.
using System; class Program { static void Main() { double balance = 5000; double withdrawal = 3000; if (withdrawal <= balance) { balance -= withdrawal; Console.WriteLine("Withdrawal successful! Remaining balance: " + balance); } else { Console.WriteLine("Insufficient funds."); } } }
When the above code is compiled and executed, it produces the following result −
Withdrawal successful! Remaining balance: 2000
Example: Checking Student Pass/Fail
In the following example, we are using relational operators to determine whether a student has passed or failed an exam.
using System; class Program { static void Main() { int marks = 45; if (marks >= 50) { Console.WriteLine("You passed the exam."); } else { Console.WriteLine("You failed. Better luck next time."); } } }
When the above code is compiled and executed, it produces the following result −
You failed. Better luck next time.
Best Practices
The following are the best practices while working with comparisons:
- You must use == for comparisons, not = (assignment).
- You should use && and || instead of invalid chained comparisons like 5 < x < 10.
- You should use string.IsNullOrEmpty(x) instead of x.Length > 0 to avoid exceptions.
- You should avoid redundant comparisons; use if (x) instead of if (x == true).