< Prev
Next >

# C++ Relational Operators

In this article, we are going to discuss another set of operators provided by C++, known as relational operators. There are six relational operators in C -

Relational Operators Description
< 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

``````// A coding example with < operator in C++

#include<iostream>

using namespace std;

int main()
{
if(5 < 10)		// comparing an int to an int
cout<< "5 is less than 10 " <<"\n";

if('a' < 'b')		// comparing a character with a character
cout<< "a is less than b " << "\n";

if(100 < 'z')		// comparing an int with ASCII value of a character
cout<< "100 is less than z " <<"\n";

if(100.5 < 100.4)	// comparing a double with double
cout<< "100.5 is less than 100.4 " << "\n";

if(100 < 100.001)	// comparing an int with a double
cout<< "100 is less than 100.001" << "\n";

return 0;
}``````

## 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

``````// A coding example with > operator in C++

#include<iostream>

using namespace std;

int main()
{
if(10 > 10)		// comparing an int with an int
cout<< "10 is greater than 10 " << "\n";

if('b' > 'a')		//  comparing a character with a character
cout<< "b is greater than a" << "\n";

if(200 > 'z')		// comparing an int with an ASCII value of A character
cout<< "200 is greater than z " << "\n";

if( 100.5f > 100.4999)	// comparing a float with a double
cout<< "100.5 is greater than 100.4999 " << "\n";

if(100 > 100.001)	// comparing an int with a double
cout<< "100 is greater than 100.001";

return 0;
}``````

## Output is

``````b is greater than a
200 is greater than z
100.5 is greater than 100.4999``````

## A coding example with <= operator

``````// A coding example with <= operator in C++

#include<iostream>

using namespace std;

int main()
{
if('a' <= 'b')		//comparing a character with another character
cout<<"a is less than or equal to b " <<"\n";

if('a' <= 1)		//comparing a character's ASCII value with int
cout<<"a is less than or equal to 1 " <<"\n";

if('z' <= 'z')
cout<<"z is less than equal to z " <<"\n";

if(30.5 <= 31)		//comparing an double value with int
cout<<"30.5 is less than or equal to 31 "<<"\n";

if(20.5 <= 20.6)	//comparing an double value with int
cout<<"20.5 is less than or equal to 20.6 " <<"\n";

if(10.5 <= 10.5)	//comparong a double with a double
cout<<"10.5 is less than or equal to 10.5 " <<"\n";

if(5 <= 5)		//comparing an int to int
cout<<"5 is less than or equal to 5 " <<"\n";

if(6 <= 6.0)		//comparing a int with a double
cout<<"6 is less than or equal to 6.0 " <<"\n";

if(10 <= 6)		//comparing a int with a double
cout<<"10 is less than or equal to 6 " <<"\n";

return 0;
}``````

## Output is

``````a is less than or equal to b
z is less than equal to z
30.5 is less than or equal to 31
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 5
6 is less than or equal to 6.0``````
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 an 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.

``````// >= greater than equals to operator in C++

#include<iostream>

using namespace std;

int main()
{
if('z' >= 'b')		// comparing a character with another character
cout<< "z is greater than or equal to b" << "\n";

if('a' >= -1)		// comparing a character's ASCII value with int
cout<< "a is greater than or equal to 1 " << "\n";

if('z' >= 'z')
cout<< "z is greater than equal to z " <<"\n";

if(50.5 >=50)		// comparing a double value with int
cout<< "50.5 is greater than or equal to 50 " << "\n";

if(19.7 >=21.7)		// comparing a double value with a double
cout<< "19.7 is greater than or equal to 21.7 " <<"\n";

if(10.5 >= 10.5)	// comparing a double with a double
cout<< "10.5 is greater or equal to 10.5 " <<"\n";

if(5 >= 5.0)		// comparing an int to int
cout<< "5 is greater than or equal to 5.0 " <<"\n";

if(6 >= 10)		// comparing a int with a double
cout<< "6 is greater than or equal to 10 " <<"\n";

if(100.5 >= 'a')	// comparing a double with a char
cout<< "100.5 is greater than or equal to a";

return 0;
}``````

## Output is

``````z is greater than or equal to b
a is greater than or equal to 1
z is greater than equal to z
50.5 is greater than or equal to 50
10.5 is greater or equal to 10.5
5 is greater than or equal to 5.0
100.5 is greater than or equal to a``````
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

``````// A coding example with == operator in C++

#include<iostream>

using namespace std;

int main()
{
if('z' == 'z') 		// comparing a character to another character
cout<< "z is equal to z" << "\n";

if(50.5 == 50.5)	// comparing an double value with double
cout<< "50.5 is equal to 50.5" << "\n";

if(100 == 100.001)	// comparing an int with a double
cout<< "100 is equal to 100.001" << "\n";

if(99.9999 == 99.999)	// comparing a double with a double
cout<< "99.9999 is  equal to 99.999 "<< "\n";

if(5 == 5.0)		// comparing an int to double
cout<< "5 is equal to 5.0" << "\n";

if(10 == 10)		// comparing an int to int
cout<< "10 is equal to 10 " << "\n";

if(97 == 'a')		// comparing an int to ASCII value of character
cout<< "97 is equal to a";

return 0;
}``````

## Output -

``````z is equal to z
50.5 is equal to 50.5
5 is equal to 5.0
10 is equal to 10
97 is equal to a``````
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

``````// A coding example with != operator in C++

#include<iostream>

using namespace std;

int main()
{
if('z' != 'a') 		// comparing a character to another character
cout<< "z is not equal to a " <<"\n";

if(100 != 100.001)	// comparing an int with a double
cout<< "100 is not equal to 100.001" << "\n";

if(99.9999 != 99.999) 	// comparing a double with a double
cout<< "99.9999 is  not equal to 99.999 " << "\n";

if(5.00 != 5.0)		// comparing an int to double
cout<< "5.00 is not equal to 5.0 " << "\n";

if(11 !=10)
cout<< "11 is not equal to 10 " << "\n";

if(98 != 'a')		// comparing int to ASCII value of a character
cout<< "98 is not equal to a";

return 0;
}``````

## Output -

``````z is not equal to a
100 is not equal to 100.001
99.9999 is  not equal to 99.999
11 is equal to 10
98 is equal to a``````
Note : There should never be any space between ! and = of != operator, or a compile error is thrown.   