Relational Operators in C#.Net



In this asp.net c# post we will learn relational operators with example.

Relational Operators in C#.Net


Relational operator also known as comparison operators.
The comparison operators used to compare two operands and returns true or false based on comparison.

C# Supports following Comparison operators

Assume int variable X=10 and variable Y=5 then

ID OPERATOR DETAIL EXAMPLE
1 == Equal equal to  if(X == Y) then false
2 != Not equal to  if(X != Y) then true
3 < Less than  if(X < Y) then false
4 > Greater than  if(X > Y) then true
5 <= Less than or equal to  if(X <= Y) then false
6 >= Greater than or equal to  if(X >= Y) then true

Equal == comparison operator

Example

int a = 5;
int b = 5;

if(a == b)
{
“a is equal to b”;
}
else
{
“a is not equal to b”
}

The output is :
a is equal to b

In above c# example a and b both are same, then if condition return true and print “a is equal to b” in output.


Not Equal != comparison operator

Example

int a = 5;
int b = 5;

if(a != b)
{
“a is equal to b”;
}
else
{
“a is not equal to b”
}

The output is :
a is not equal to b

In above c# example a and b both are same, then if condition return false and print else portion “a is not equal to b” in output because we use not equal operator.


< Less than and <= Less than or Equal to operators

Example

int a = 15;
int b = 5;

if(a < b)
{
“a is smaller than b”;
}
else if(a <= b)
{
“a is equal to b”;
}
else
{
 “a is bigger than b”;
}

The output is :
a is bigger than b

Here, a=15 and b=5, so a is not smaller and not equal to b. The both if condition not satisfy then the result will be last else portion “a is bigger than b”.


> Greater than and >= Greater than or Equal to operators

Example

int a = 15;
int b = 5;

if(a > b)
{
 “a is bigger than b”;
}
else if(a >= b)
{
“a is equal to b”;
}
else
{
“a is smaller than b”;
}

The output is :
a is bigger than b

In above c# example the result is “a is bigger than b” because a=15 value bigger than b=5 value.

The first if condition returns true if only a is greater than b. If suppose a=5 and b=5, then the second if condition will be true and result is “a is equal to b”.

Related ASP.Net Topics :

Arithmetic Operators in C#.Net
Logical Operators in C#.Net


Subscribe us

If you liked this asp.net post, then please subscribe to our YouTube Channel for more asp.net video tutorials.

We hope that this asp.net tutorial helped you to understand about Relational Operators in C#.


Next asp.net tutorial we will learn about Assignment Operators.


Leave a Reply

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