C++ Relational Operators



C++ Relational operators specify the relation between two variables by comparing them. There are 5 relational operators in C

C++ Relational Operators

In C++ Programming, the values stored in two variables can be compared using following operators and relation between them can be determined.

Various C++ relational operators available are-

Operator, Meaning
> ,Greater than
>= , Greater than or equal to
== , Is equal to
!= , Is not equal to
< , Less than or equal to <= , Less than [/table]

Relational Operator Programs

Program #1: Relational Operator Comparison

As we discussed earlier, C++ Relational Operators are used to compare values of two variables. Here in example we used the operators in if statement.

Now if the result after comparison of two variables is True, then if statement returns value 1.

And if the result after comparison of two variables is False, then if statement returns value 0.

#include<iostream>
using namespace std;

int main()
{
   int a=10,b=20,c=10;

   if(a>b)
      cout<<"a is greater"<<endl;

   if(a<b)
      cout<<"a is smaller"<<endl;

   if(a<=c)
      cout<<"a is less than/equal to c"<<endl;

   if(a>=c)
      cout<<"a is less than/equal to c"<<endl;

   return 0;
}

Output

a is smaller 
a is less than/equal to c
a is greater than/equal to c

Program #2: Relational Operator Equality

In C++ Relational operators, two operators that is == (Is Equal to) and != (is Not Equal To), are used to check whether the two variables to be compared are equal or not.

Let us take one example which demonstrate this two operators.

#include<iostream>
using namespace std;

int main()
{
   int num1 = 30;
   int num2 = 40;
   int num3 = 40;
	
   if(num1!=num2)
      cout<<"num1 Is Not Equal To num2"<<endl;
		
   if(num2==num3)
      cout<<"num2 Is Equal To num3"<<endl;
		
   return(0);
}

Output

num1 Is Not Equal To num2
num2 Is Equal To num3