Home » C# Relational Operators : A Tutorial

C# Relational Operators : A Tutorial

by wskandot71

Relational operators in C# are used to compare two values and return a Boolean (true or false) result.

They are commonly used in conditional statements, loops, and decision-making constructs.

This tutorial will cover:

  • List of Relational Operators in C#
  • Using Relational Operators with Examples
  • Operator Precedence in Relational Expressions
  • Relational Operators in Conditional Statements and Loops
  • Best Practices for Using Relational Operators

1. List of Relational Operators in C#

Relational operators compare two values and return true or false.

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

2. Using Relational Operators with Examples

Equality Operator (==)

The == operator checks if two values are equal.

int x = 5;
int y = 5;

bool result = (x == y);
Console.WriteLine(result); // Output: True
  • Returns true if both values are equal, otherwise false.

Inequality Operator (!=)

The != operator checks if two values are not equal.

int x = 5;
int y = 10;

bool result = (x != y);
Console.WriteLine(result); // Output: True
  • Returns true if values are not equal, otherwise false.

Greater Than (>)

The > operator checks if the left value is greater than the right value.

int a = 10;
int b = 5;

bool result = (a > b);
Console.WriteLine(result); // Output: True

Less Than (<)

The < operator checks if the left value is less than the right value.

int a = 10;
int b = 20;

bool result = (a < b);
Console.WriteLine(result); // Output: True

Greater Than or Equal To (>=)

The >= operator checks if the left value is greater than or equal to the right value.

int a = 10;
int b = 10;

bool result = (a >= b);
Console.WriteLine(result); // Output: True

Less Than or Equal To (<=)

The <= operator checks if the left value is less than or equal to the right value.

int a = 5;
int b = 10;

bool result = (a <= b);
Console.WriteLine(result); // Output: True

3. Operator Precedence in Relational Expressions

Relational operators have lower precedence than arithmetic operators but higher precedence than logical operators.

Operator Precedence Table

Operator Description Precedence
*, /, % Multiplication, Division, Modulus Highest
+, – Addition, Subtraction Medium
<, <=, >, >= Relational Operators Lower
==, != Equality Operators Lower
&&, ` `

Example: Operator Precedence

int x = 10;
int y = 5;
int z = 2;

bool result = x > y + z;
Console.WriteLine(result); // Output: True
  • y + z is evaluated first (5 + 2 = 7).
  • x > 7 results in true.

Using Parentheses for Clarity

bool result = (x > y) && (y > z);
  • Parentheses improve readability.

4. Relational Operators in Conditional Statements and Loops

Relational operators are frequently used in if statements and loops.

Example: Using Relational Operators in an if Statement

int age = 18;

if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
else
{
    Console.WriteLine("You are a minor.");
}
  • If age is 18 or greater, “You are an adult.” is printed.

Example: Using Relational Operators in a while Loop

int count = 0;

while (count < 5)
{
    Console.WriteLine("Count is: " + count);
    count++;
}
  • The loop runs while count is less than 5.

Example: Using Relational Operators in a for Loop

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine("Iteration: " + i);
}
  • The loop executes while i is less than or equal to 5.

5. Comparing Different Data Types

Relational operators can compare different data types, but type compatibility must be considered.

Example: Comparing int and double

int a = 10;
double b = 10.0;

bool result = (a == b);
Console.WriteLine(result); // Output: True
  • Implicit conversion occurs, and 10 is treated as 10.0.

Example: Comparing Strings

String comparison uses == for equality.

string str1 = "Hello";
string str2 = "Hello";

bool result = (str1 == str2);
Console.WriteLine(result); // Output: True

Comparing Objects (Equals Method)

For objects, use .Equals() instead of ==.

object obj1 = "Hello";
object obj2 = "Hello";

bool result = obj1.Equals(obj2);
Console.WriteLine(result); // Output: True

6. Best Practices for Using Relational Operators

Use Proper Data Types for Comparisons

double price = 99.99;
int discount = 10;
bool result = price > discount; // Valid

Use Parentheses for Complex Conditions

if ((age >= 18) && (age <= 65))
{
    Console.WriteLine("Eligible for work.");
}

Avoid Using == to Compare Floating-Point Numbers

double x = 0.1 + 0.2;
if (Math.Abs(x - 0.3) < 0.0001) // Better approach
{
    Console.WriteLine("Equal");
}
  • Floating-point comparisons should use Math.Abs().

Use Equals() for Object Comparisons

if (obj1.Equals(obj2)) // Better than obj1 == obj2

Conclusion

  • Relational operators compare values and return a Boolean result.
  • ==, != check equality, while <, >, <=, >= check ordering.
  • Relational operators are commonly used in conditions and loops.
  • Operator precedence affects evaluation order.
  • Best practices help avoid common mistakes in comparisons.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.