Relational operators in C++ are used to compare two values or expressions and return a Boolean result, either true or false. These operators are commonly used in conditional statements, loops, and decision-making operations.
- C++ provides six relational operators: >, <, >=, <=, ==, and !=.
- Relational operators return a value of type bool, where true is displayed as 1 and false as 0 by default when printed using cout.
- They are used in control flow statements such as if, while, and for.
Syntax
All C++ relational operators are binary operators that are used with two operands as shown:
operand1 relational_operator operand2
expression1 relational_operator expression2
Types of Relational Operators in C++
C++ provides six relational operators for comparing values.

1. Greater than ( > )
The greater than ( > ) operator returns true if the left operand is greater than the right operand, otherwise, it returns false.
Example:
29 > 21 // returns true
12 > 12 // return false
10 > 57 // return false
2. Less than ( < )
The less than ( < ) operator checks if the left operand is less than the right operand. If it is, it returns true, otherwise, it returns false.
Example:
2 < 21 // returns true
12 < 12 // return false
12 < 5 // return false
3. Greater than or equal to ( >= )
The greater than or equal to ( >= ) operator returns true if the left operand is greater than or equal to the right operand, otherwise, it returns false.
Example:
29 >= 21 // returns true
12 >= 12 // return true
10 >= 58 // return false
4. Less than or equal to ( <= )
The less than or equal to ( <= ) operator returns true if the left operand is less than or equal to the right operand, otherwise, it returns false.
Example:
2 <= 21 // returns true
12 <= 12 // return true
12 <= 5 // return false
5. Equal to ( == )
The equal to ( == ) operator checks if two values are equal. It returns true if the values are equal, otherwise, it returns false.
Example:
9 == 9 // returns true
19 == 12 // return false
6. Not equal to ( != )
The not equal to ( Â != Â ) operator checks if two values are not equal. It returns true if the values are different and false if they are equal.
Example:
12 != 21 // returns true
12 != 12 // return false
Example of Relational Operators in C++
The following program demonstrates all six relational operators by comparing two integer variables.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << "a > b: " << (a > b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a >= b: " << (a >= b) << endl;
cout << "a <= b: " << (a <= b) << endl;
cout << "a == b: " << (a == b) << endl;
cout << "a != b: " << (a != b) << endl;
return 0;
}
Output
a > b: 0 a < b: 1 a >= b: 0 a <= b: 1 a == b: 0 a != b: 1
Explanation:
- a > b returns false because 10 is not greater than 20.
- a < b returns true because 10 is less than 20.
- a >= b returns false because 10 is neither greater than nor equal to 20.
- a <= b returns true because 10 is less than 20.
- a == b returns false because the values are different.
- a != b returns true because 10 and 20 are not equal.
By default, cout displays Boolean values as 1 for true and 0 for false. To display true and false as words, use std::boolalpha.