Code

Relational Operators

Relational operators, also called comparison operators, are used to compare two or more values (operands) and always return Boolean type. That is, the result of the comparison is either True or False.

Relational Operators in C#

Operator Description Examples
> Greater than Return true, if operand on the left is greater than the operand on the right. Otherwise, return false. a > b
< Less than Return true, if operand on the left side is less than the operand on the right side. Otherwise, return false. a < b
== Equal to Return true, if operand on the left side is equal to the operand on the right side. Otherwise, return false. a == b
!= Not equal to Return true, if operand on the left side is not equal to the operand on the right side. Otherwise, return false. a != b
>= Greater than or equal to Return true, if operand on the left side is greater than, or equal to, the operand on the right side. Otherwise, return false. a >= b
<= Less than or equal to Return true, if operand on the left side is less than or equal to the operand on the right side. Otherwise, return false. a <= b

Suppose variable a has a value 1 and variable b has a value of 2:

int a = 1;
int b = 2;

// True if a is equal to b
Console.WriteLine($"a == b : {a == b}");

// True if a is not equal to b
Console.WriteLine($"a != b : {a != b }");

// True if a is less than b 
Console.WriteLine($"a < b : {a < b}");

// True if a is less than or equal to b
Console.WriteLine($"a <= b : {a <= b}"); 

// True if a is greater than b</pre>
Console.WriteLine($"a > b : {a > b}");

// True if a is greater than or equal to b
Console.WriteLine($"a >= b : {a >= b}");
OUTPUT

a == b : False
a != b : True
a < b : True
a <= b : True
a > b : False
a >= b : False

Run Demo

Tagged in :

Add Comment

Click here to post a comment

Subscribe

Most Popular

Popular Video