In this tutorial, you shall learn about different Relational Operators available in C++ programming language and go through each of these Relational Operations in detail, with the help of examples.
Relational Operators in C++
C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc.
Relational Operators are also called Comparison Operators.
The syntax of any Relational Operator with operands is
operand1 operator_symbol operand2
For example, to check if x
and y
are equal, we use the following expression.
x == y
Operator Symbol – Name – Example – Description
The following table specifies symbol, example, and description for each of the Assignment Operator in C++.
Operator Symbol | Relational Operation | Example | Description |
== | Equal to | x == y | Returns true if x is equal to y, else it returns false. |
!= | Not equal | x != y | Returns true if x is not equal to y, else it returns false. |
> | Greater than | x > y | Returns true if x is greater than y, else it returns false. |
< | Less than | x < y | Returns true if x is less than y, else it returns false. |
>= | Greater than or equal to | x >= y | Returns true if x is greater than or equal to y, else it returns false. |
<= | Less than or equal to | x <= y | Returns true if x is less than or equal to y, else it returns false. |
Since Relational Operators return boolean value, they can be used as conditions in Conditional Statements.
We can compare any two values or objects, but these values or objects must be comparable by C++.
1. Equal-to Operator
In the following example, we take two values in x
and y
, and programmatically check if x
equals y
using Equal to Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 5;
if (x == y) {
cout << "x and y are equal." << endl;
} else {
cout << "x and y are not equal." << endl;
}
}
Output
x and y are equal.
Program ended with exit code: 0
2. Not-Equal Operator
In the following example, we take two values in x
and y
, and programmatically check if x
does not equal y
using Not Equal Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 4;
if (x != y) {
cout << "x and y are not equal." << endl;
} else {
cout << "x and y are equal." << endl;
}
}
Output
x and y are not equal.
Program ended with exit code: 0
3. Greater-than Operator
In the following example, we take two values in x
and y
, and programmatically check if x
is greater than y
using Greater than Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 4;
if (x > y) {
cout << "x is greater than y." << endl;
} else {
cout << "x is not greater than y." << endl;
}
}
Output
x is greater than y.
Program ended with exit code: 0
4. Less-than Operator
In the following example, we take two values in x
and y
, and programmatically check if x
is less than y
using Less than Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 2;
int y = 4;
if (x < y) {
cout << "x is less than y." << endl;
} else {
cout << "x is not less than y." << endl;
}
}
Output
x is less than y.
Program ended with exit code: 0
5. Greater-than or equal-to Operator
In the following example, we take two values in x
and y
, and programmatically check if x
is greater than or equal to y
using Greater than or equal to Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 4;
if (x >= y) {
cout << "x is greater than or equal to y." << endl;
} else {
cout << "x is not greater than or equal to y." << endl;
}
}
Output
x is greater than or equal to y.
Program ended with exit code: 0
6. Less-than or equal-to Operator
In the following example, we take two values in x
and y
, and programmatically check if x
is less than or equal to y
using Less than or equal to Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 2;
int y = 4;
if (x <= y) {
cout << "x is less than or equal to y." << endl;
} else {
cout << "x is not less than or equal to y." << endl;
}
}
Output
x is less than or equal to y.
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned what Relational Operators are, and how to use them in C++ programs, with the help of examples.