Relational operators are used to compare two values or expressions. The result of a relational comparison is a Boolean value: true or false.
Relational operators compare two values and determine their relationship.
int a = 10;
int b = 20;
bool result = a < b;
Here, the result is true because 10 is less than 20.
| Operator | Name |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
The == operator checks whether two values are equal.
int a = 10;
int b = 10;
bool result = (a == b);
The result is true.
int a = 10;
int b = 20;
bool result = (a == b);
The result is false because the two values are different.
The != operator checks whether two values are not equal.
int a = 10;
int b = 20;
bool result = (a != b);
The result is true.
int a = 10;
int b = 10;
bool result = (a != b);
The result is false because both values are equal.
The > operator checks whether the left value is greater than the right value.
int a = 20;
int b = 10;
bool result = (a > b);
The result is true.
int a = 10;
int b = 20;
bool result = (a > b);
The result is false because 10 is not greater than 20.
The < operator checks whether the left value is less than the right value.
int a = 10;
int b = 20;
bool result = (a < b);
The result is true.
int a = 20;
int b = 10;
bool result = (a < b);
The result is false.
The >= operator checks whether the left value is greater than or equal to the right value.
int marks = 40;
bool passed = (marks >= 40);
The result is true.
int marks = 80;
bool passed = (marks >= 40);
The result is true because 80 is greater than 40.
The <= operator checks whether the left value is less than or equal to the right value.
int age = 18;
bool result = (age <= 18);
The result is true.
int age = 15;
bool result = (age <= 18);
The result is true because 15 is less than 18.
A relational expression produces a Boolean result.
int a = 10;
int b = 20;
bool result = a < b;
The variable result stores either true or false.
#include <iostream>
int main() {
int a = 10;
int b = 20;
std::cout << (a < b);
return 0;
}
By default, Boolean output is commonly displayed as 1 for true and 0 for false.
The std::boolalpha manipulator can display Boolean results as true and false.
#include <iostream>
int main() {
int a = 10;
int b = 20;
std::cout << std::boolalpha;
std::cout << (a < b);
return 0;
}
Relational operators can compare variables.
int age1 = 20;
int age2 = 25;
if (age1 < age2) {
std::cout << "Age1 is smaller";
}
The condition is true, so the message is displayed.
Relational operators are frequently used inside if conditions.
int age = 20;
if (age >= 18) {
std::cout << "Adult";
}
The message is displayed because the condition is true.
int marks = 75;
if (marks >= 40) {
std::cout << "Pass";
}
The >= operator checks whether the marks are at least 40.
#include <iostream>
int main() {
int a = 50;
int b = 30;
if (a > b) {
std::cout << "A is greater than B";
}
return 0;
}
Relational operators can also compare floating-point values.
double price1 = 99.50;
double price2 = 100.00;
if (price1 < price2) {
std::cout << "Price1 is lower";
}
For floating-point comparisons, be careful when exact equality is required because of representation and rounding.
Characters can be compared using relational operators according to their values in the execution character set.
char a = 'A';
char b = 'B';
if (a < b) {
std::cout << "A comes before B";
}
C++ std::string objects support relational comparisons.
#include <iostream>
#include <string>
int main() {
std::string a = "Apple";
std::string b = "Banana";
if (a < b) {
std::cout << "A comes before B";
}
return 0;
}
String comparisons are based on the ordering defined for std::string.
Relational expressions can be combined with logical operators.
int age = 25;
if (age >= 18 && age <= 60) {
std::cout << "Age is within the range";
}
Here, both relational conditions must be true.
A common beginner mistake is confusing assignment = with equality comparison ==.
int age = 20;
if (age == 20) {
std::cout << "Age is 20";
}
Use = for assignment and == for equality comparison.
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
if (number > 0) {
std::cout << "Positive number";
} else if (number < 0) {
std::cout << "Negative number";
} else {
std::cout << "Zero";
}
return 0;
}
The program uses relational operators to compare the input with zero.
#include <iostream>
int main() {
int a = 20;
int b = 10;
std::cout << std::boolalpha;
std::cout << "a == b: "
<< (a == b) << std::endl;
std::cout << "a != b: "
<< (a != b) << std::endl;
std::cout << "a > b: "
<< (a > b) << std::endl;
std::cout << "a < b: "
<< (a < b) << std::endl;
std::cout << "a >= b: "
<< (a >= b) << std::endl;
std::cout << "a <= b: "
<< (a <= b) << std::endl;
return 0;
}
| Operator | Name | Example | Meaning |
|---|---|---|---|
| == | Equal to | a == b |
a and b have equal values |
| != | Not equal to | a != b |
a and b have different values |
| > | Greater than | a > b |
a is greater than b |
| < | Less than | a < b |
a is less than b |
| >= | Greater than or equal | a >= b |
a is greater than or equal to b |
| <= | Less than or equal | a <= b |
a is less than or equal to b |
Question: Which operator is used to check whether two values are equal in C++?