< Prev
Next >

# Relational Operators

In C# language, there are six relational operators:-
• <(less than operator)
• >(greater than operator)
• <=(less than equals to operator)
• >=(greater than equals to operator)
• ==(equals to operator)
• !=(not equals to operator)

## < (less than operator)

This operator is also known as less than operator. It compares between the values of two operands around it. < operator returns a boolean value true when operand on its left is less than the operand on its right, otherwise it returns false.

## A coding example with < operator

``````// C# - Example of < relational operator

using System;

class A
{
public static void Main()
{
//Local variables
int a = 5, b =10, c = 100;
char ch1 = 'a', ch2 = 'b';
float f1 = 100.5f, f2 = 100.4f;
double d = 100.001;

//comparing an int to an int
if(a < b)
Console.WriteLine("{0} is less than {1}", a , b);

//comparing a character with a character
if(ch1 < ch2)
Console.WriteLine("{0} is less than {1}", ch1, ch2);

//comparing an int with ASCII value of a character
if(c < 'z')
Console.WriteLine("{0} is less than z", c);

//comparing a float with float
if(f1 < f2)
Console.WriteLine("{0} is less than {1}", f1, f2);

//comparing an int with a double
if(c < d)
Console.WriteLine("{0} is less than {1}",c, d);
}
}``````

## Output is

``````5 is less than 10
a is less than b
100 is less than z
100 is less than 100.001``````

## > (greater than operator)

This operator is also known as greater than operator. It compares between the values of two operands around it. > operator returns a boolean value true when operand on its left is greater than the operand on its right, otherwise it returns false.

## A coding example with > operator

``````// C# - Example of > relational operator

using System;

class A
{
public static void Main()
{
//Local variables
int a = 10, b =10, c = 200;
char ch1 = 'a', ch2 = 'b';
float f1 = 100.5f, f2 = 100.4999f;
double d = 100.001;

//comparing an int to an int
if(a > b)
Console.WriteLine("{0} is greater than {1}", a , b);

//comparing a character with a character
if(ch1 > ch2)
Console.WriteLine("{0} is greater than {1}", ch1, ch2);

//comparing an int with ASCII value of a character
if(c > 'z')
Console.WriteLine("{0} is greater than z", c);

//comparing a float with float
if(f1 > f2)
Console.WriteLine("{0} is greater than {1}", f1, f2);

//comparing an int with a double
if(c > d)
Console.WriteLine("{0} is greater than {1}",c, d);
}
}``````

## Output is

``````200 is greater than z
100.5 is greater than 100.4999
200 is greater than 100.001``````

## <= (less than equals to operator)

This operator is also known as less than equal to operator. It compares between the values of two operands around it and it returns a boolean value, true, if either of these conditions are true -
• If the operand of the left of <= operator is smaller than operand on the right of <=, or,
• If the operand on the left of <= is equal to operand on its right.
• if neither of above conditions are met then this operator return false.

## A coding example with <= operator

``````// C# - Example of <= relational operator

using System;

class A
{
public static void Main()
{
//Local variables
int a = 5, b = 6, c = 10;
char ch1 = 'a', ch2 = 'b';
float f1 = 6.0f, f2 = 20.6f;
double d1 = 10.5, d2 = 20.5;

//comparing a character with another character
if(ch1 <= ch2)
Console.WriteLine("{0} is less than or equal to {1}", a, b);

//comparing a character's ASCII value with int
if(ch1 <= 100)
Console.WriteLine("{0} is less than or equal to 100", ch1, 100);

//comparing a character with another character
if(ch2 <= 'b')
Console.WriteLine("{0} is less than equal to {1}", ch2, 'b');

//comparing an double value with a float
if(d2 <= f2)
Console.WriteLine("{0} is less than or equal to {1}", d2, f2);

//comparing a double with a double
if(d1 <= 10.5)
Console.WriteLine("{0} is less than or equal to {1}", d1, 10.5);

//comparing an int to int
if(a <= b)
Console.WriteLine("{0} is less than or equal to {1}", a, b);

//comparing an  int with a float
if(b <= 7.5f)
Console.WriteLine("{0} is less than or equal to {1}", b, 7.5f);

//comparing an  int with a float
if(b <= f1)
Console.WriteLine("{0} is less than or equal to {1}", b, f1);

//comparing an  int with a double
if(c <= d1)
Console.WriteLine("{0} is less than or equal to {1}", c, d1);
}
}``````

## Output is

``````5 is less than or equal to 6
a is less than or equal to 100
b is less than equal to b
20.5 is less than or equal to 20.6
10.5 is less than or equal to 10.5
5 is less than or equal to 6
6 is less than or equal to 7.5
6 is less than or equal to 6
10 is less than or equal to 10.5``````

Note: There should never be any space between < and = of <= operator, or a compile error is thrown.

## >= (greater than equals to operator)

This operator is also known as greater than equal to operator. It compares between the values of two operands around it and it returns a boolean value, true, if either of these conditions are true -
• If the operand of the left of >= operator is greater than operand on the right of >=, or,
• If the operand on the left of >= is equal to operand on its right.
• if neither of above conditions are met then this operator return false.

## A coding example with >= operator

``````// C# - Example of >= relational operator

using System;

class A
{
public static void Main()
{
//Local variables
int a = 5, b = 6, c = 10;
char ch1 = 'x', ch2 = 'e';
float f1 = 6.0f, f2 = 20.6f;
double d1 = 10.5, d2 = 30.5;

//comparing a character with another character
if(ch1 >= ch2)
Console.WriteLine("{0} is greater than or equal to {1}", ch1, ch2);

//comparing a character's ASCII value with int
if(ch1 >= 10)
Console.WriteLine("{0} is greater than or equal to 10", ch1, 10);

//comparing a character with another character
if(ch2 >= 'b')
Console.WriteLine("{0} is greater than equal to {1}", ch2, 'b');

//comparing an double value with a float
if(d2 >= f2)
Console.WriteLine("{0} is greater than or equal to {1}", d2, f2);

//comparong a double with a double
if(d1 >= 10.5)
Console.WriteLine("{0} is greater than or equal to {1}", d1, 10.5);

//comparing an int to int
if(a >= b)
Console.WriteLine("{0} is greater than or equal to {1}", a, b);

//comparing an  int with a float
if(b >= 3.5f)
Console.WriteLine("{0} is greater than or equal to {1}", b, 3.5f);

//comparing an  int with a float
if(b >= f1)
Console.WriteLine("{0} is greater than or equal to {1}", b, f1);

//comparing an  int with a double
if(c >= d1)
Console.WriteLine("{0} is greater than or equal to {1}", c, d1);
}
}``````

## Output is

``````x is greater than or equal to e
x is greater than or equal to 10
e is greater than equal to b
30.5 is greater than or equal to 20.6
10.5 is greater than or equal to 10.5
6 is greater than or equal to 3.5
6 is greater than or equal to 6``````

Note: There should never be any space between > and = of >= operator, or a compile error is thrown.

## == (equals to operator)

This operator is called equals to operator. It compares between the values of two operands by checking for their equality. It returns a boolean value true when operands have equal values otherwise it returns false.

## A coding example with == operator

``````// C# - Example of == relational operator

using System;

class A
{
public static void Main()
{
//Local variables
int a = 100, b = 5;
char ch1 = 'z', ch2 = 'a';
float f2 = 99.99f;
double d1 = 50.5, d2 = 99.9999;

//comparing a character with another character
if(ch1 == 'z')
Console.WriteLine("{0} is greater than or equal to {1}", ch1, 'z');

//comparing a double value with a double
if(d1 == 50.5)
Console.WriteLine("{0} is greater than or equal to 50.5", d1, 50.5);

//comparing an  int with another double
if(a == 100.001)
Console.WriteLine("{0} is greater than equal to 100.001", a, 100.001);

//comparing an double value with a float
if(d2 == f2)
Console.WriteLine("{0} is greater than or equal to {1}", d2, f2);

//comparong a double with a double
if(d1 == 10.5)
Console.WriteLine("{0} is greater than or equal to {1}", d1, 10.5);

//comparing an int to float
if(b == 4.999f)
Console.WriteLine("{0} is greater than or equal to {1}", b, 4.999f);

//comparing an  int with a double
if(ch2 == 97)
Console.WriteLine("{0} is greater than or equal to {1}", ch2, 97);
}
}``````

## Output -

``````z is greater than or equal to z
50.5 is greater than or equal to 50.5
a is greater than or equal to 97``````

Note: There should never be any space between = and = of == operator, or a compile error is thrown.

## != (not equals to operator)

This operator is also knows an not equal to operator. It compares between the values of its two operands by checking for their inequality. It returns a boolean value true when operands have unequal values otherwise it returns false.

## A coding example with != operator

``````// C# - Example of != relational operator

using System;

class A
{
public static void Main()
{
//Local variables
int a = 100, b = 5;
char ch1 = 'z', ch2 = 'a';
float f2 = 99.999f;
double d1 = 50.5, d2 = 99.9999;

//comparing a character with another character
if(ch1 != 'z')
Console.WriteLine("{0} is not equal  to {1}", ch1, 'z');

//comparing a character with another character
if(ch1 != ch2)
Console.WriteLine("{0} is not equal  to {1}", ch1, ch2);

//comparing a double value with a double
if(d1 != 50.4)
Console.WriteLine("{0} is not equal  to 50.4", d1, 50.4);

//comparing an  int with another double
if(a != 100.001)
Console.WriteLine("{0} is greater than equal to 100.001", a, 100.001);

//comparing an double value with a float
if(d2 != f2)
Console.WriteLine("{0} is not equal  to {1}", d2, f2);

//comparong a double with a double
if(d1 != 10.5)
Console.WriteLine("{0} is not equal  to {1}", d1, 10.5);

//comparing an int to float
if(b != 4.999f)
Console.WriteLine("{0} is not equal  to {1}", b, 4.999f);

//comparing an  int with a double
if(ch2 != 97)
Console.WriteLine("{0} is not equal  to {1}", ch2, 97);
}
}``````

## Output -

``````z is not equal  to a
50.5 is not equal  to 50.4
100 is greater than equal to 100.001
99.9999 is not equal  to 99.999
50.5 is not equal  to 10.5
5 is not equal  to 4.999``````

Note : There should never be any space between ! and = of != operator, or a compile error is thrown.

< Prev
Next >
< C# Compound Assignment Operators
C# Logical Operators >   