C# Relational Operator

https://‮‬www.theitroad.com

In C#, relational operators are used to compare two values and determine whether they are equal, not equal, less than, greater than, less than or equal to, or greater than or equal to. The result of a relational operation is a boolean value (true or false).

Here are the relational operators in C#:

  • == (equal to)
  • != (not equal to)
  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)

Here are some examples of how to use relational operators in C#:

int x = 10;
int y = 5;

bool isEqual = x == y;    // isEqual is false
bool isNotEqual = x != y;    // isNotEqual is true
bool isLessThan = x < y;    // isLessThan is false
bool isGreaterThan = x > y;    // isGreaterThan is true
bool isLessThanOrEqual = x <= y;    // isLessThanOrEqual is false
bool isGreaterThanOrEqual = x >= y;    // isGreaterThanOrEqual is true

Relational operators are commonly used in conditional statements and loops to control the flow of a program based on the comparison of values. It's important to understand how to use relational operators correctly and to be aware of the potential for errors or unexpected results when comparing values of different types.