C# Relational Operators

Introduction

Relational operators are used to compare two values or expressions. The result of a relational operation is a Boolean value (true or false). These operators are essential for decision-making in programming, such as in conditional statements and loops.

List of Relational Operators

  1. Equal to (==)
  2. Not equal to (!=)
  3. Greater than (>)
  4. Less than (<)
  5. Greater than or equal to (>=)
  6. Less than or equal to (<=)

1. Equal to (==)

The equal to operator checks if two operands are equal. If they are, it returns true; otherwise, it returns false.

Syntax

result = operand1 == operand2;

Example

int a = 5;
int b = 5;
bool isEqual = (a == b);
Console.WriteLine("Is Equal: " + isEqual);

Output:

Is Equal: True

2. Not equal to (!=)

The not equal to operator checks if two operands are not equal. If they are not, it returns true; otherwise, it returns false.

Syntax

result = operand1 != operand2;

Example

int a = 5;
int b = 3;
bool isNotEqual = (a != b);
Console.WriteLine("Is Not Equal: " + isNotEqual);

Output:

Is Not Equal: True

3. Greater than (>)

The greater than operator checks if the first operand is greater than the second operand. If it is, it returns true; otherwise, it returns false.

Syntax

result = operand1 > operand2;

Example

int a = 5;
int b = 3;
bool isGreater = (a > b);
Console.WriteLine("Is Greater: " + isGreater);

Output:

Is Greater: True

4. Less than (<)

The less than operator checks if the first operand is less than the second operand. If it is, it returns true; otherwise, it returns false.

Syntax

result = operand1 < operand2;

Example

int a = 5;
int b = 8;
bool isLess = (a < b);
Console.WriteLine("Is Less: " + isLess);

Output:

Is Less: True

5. Greater than or equal to (>=)

The greater than or equal to operator checks if the first operand is greater than or equal to the second operand. If it is, it returns true; otherwise, it returns false.

Syntax

result = operand1 >= operand2;

Example

int a = 5;
int b = 5;
bool isGreaterOrEqual = (a >= b);
Console.WriteLine("Is Greater or Equal: " + isGreaterOrEqual);

Output:

Is Greater or Equal: True

6. Less than or equal to (<=)

The less than or equal to operator checks if the first operand is less than or equal to the second operand. If it is, it returns true; otherwise, it returns false.

Syntax

result = operand1 <= operand2;

Example

int a = 5;
int b = 5;
bool isLessOrEqual = (a <= b);
Console.WriteLine("Is Less or Equal: " + isLessOrEqual);

Output:

Is Less or Equal: True

Combining Relational Operators

Relational operators can be combined with logical operators (such as &&, ||, and !) to form more complex conditions.

Example: Combining Relational and Logical Operators

int a = 5;
int b = 3;
int c = 8;
bool result = (a > b) && (c > a);
Console.WriteLine("Combined Result: " + result);

Output:

Combined Result: True

Example: Using Relational Operators in Conditional Statements

Relational operators are commonly used in if statements to control the flow of a program based on conditions.

int age = 20;
if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
else
{
    Console.WriteLine("You are a minor.");
}

Output:

You are an adult.

Conclusion

Relational operators are essential for comparing values and making decisions in C# programs. By understanding and using these operators, you can control the flow of your program and perform complex logical operations. Whether you are checking for equality, inequality, or comparing the relative size of values, relational operators provide the tools you need to build robust and dynamic applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top