Updated March 23, 2023
Introduction on Relational Operators in C++
Relational operators are also known for comparison operators. Relational operators are used to relating the condition, that is it compares the two values and prints the result. In this article, we are going to see those relational operators in C++ with the help of examples.
Different Relational Operators in C++
There are total 6 relational operators ==, !=, <, >,<=, >= which are explained below:
1. Less than Operator (<)
This operator is called less-than the operator. It checks whether the value of the left operand is less than the value of the right operand or not. If it satisfies the condition then, it returns true as a value else it returns false.
2. Greater than Operator (>)
This operator is called greater than the operator. It checks whether the value of the left operand is greater than the value of the right operand. If it satisfies the condition it returns true as value else it returns false.
3. Less than or Equal to Operator (<=)
This operator is called less than or equal to the operator. It checks whether the value of the left operand is less than or equal to the value of the right operand. If it satisfies the condition it returns true as value else it returns false.
4. Greater than or Equal to Operator (>=)
This operator is called as greater than or equal to the operator. It checks whether the value of the left operand is greater than or equal to the value of the right operand. If it satisfies the condition it returns true as value else it returns false.
5. Equal to Operator (==)
This operator is called as is equal to the operator. It checks whether the value of the left operand is equal to the value of the right operand. If it satisfies the condition it returns true as value else it returns false.
6. Not Equal to Operator (!=)
This operator is called as is not equal to the operator. It checks whether the value of the left operand is not equal to the value of the right operand. If it satisfies the condition it returns true as value else it returns false.
Examples of Relational Operators in C++
Below are the different examples of relational operators in C++:
Example #1
Example to implement greater than operator is as follows.
Code:
#include <iostream>
using namespace std;
int main()
{
int X, Y;
cout << "Enter the value of X: ";
cin >> X;
cout << "Enter the value of Y: ";
cin >> Y;
if(X > Y)
{
cout << "X is greater than Y";
}
else
{
cout << "Y is greater than X";
}
return 0;
}
Output:
Example #2
Example to implement less than the operator is as follows.
Code:
#include <iostream>
using namespace std;
int main()
{
int X, Y;
cout << "Enter the value of X: ";
cin >> X;
cout << "Enter the value of Y: ";
cin >> Y;
if(X < Y)
{
cout << "X is less than Y";
}
else
{
cout << "Y is less than X";
}
return 0;
}
Output:
Example #3
An example to implement greater than or equal to the operator is as follows.
Code:
#include <iostream>
using namespace std;
int main()
{
int X, Y;
cout << "Enter the value of X: ";
cin >> X;
cout << "Enter the value of Y: ";
cin >> Y;
if(X >= Y)
{
cout << "X is greater than or equal to Y";
}
else
{
cout << "Y is greater than or equal to X";
}
return 0;
}
Output:
Example #4
Example to implement less than or equal to the operator is as follows.
Code:
#include <iostream>
using namespace std;
int main()
{
int X, Y;
cout << "Enter the value of X: ";
cin >> X;
cout << "Enter the value of Y: ";
cin >> Y;
if(X <= Y)
{
cout << "X is less than or equal Y";
}
else
{
cout << "Y is less than or equal to X";
}
return 0;
}
Output:
Example #5
Example to implement equal to operator is as follows.
Code:
#include <iostream>
using namespace std;
int main()
{
int X, Y;
cout << "Enter the value of X: ";
cin >> X;
cout << "Enter the value of Y: ";
cin >> Y;
if(X == Y)
{
cout << "X is equal to Y";
}
else
{
cout << " X is not equal to y";
}
return 0;
}
Output:
Example #6
Example to implement not equal to operator is as follows.
Code:
#include <iostream>
using namespace std;
int main()
{
int X, Y;
cout << "Enter the value of X: ";
cin >> X;
cout << "Enter the value of Y: ";
cin >> Y;
if(X != Y)
{
cout << "Condition is true";
}
else
{
cout << "condition is false";
}
return 0;
}
Output:
Recommended Articles
This is a guide to Relational Operators in C++. Here we discuss the basic concept, top 6 Relational Operators in C++ along with the examples and code implementation. You can also go through our suggested articles to learn more –