These operators are used to compare two values and return Boolean results. For example: it checks if the operand is equal to another operand or not or an operand is greater than the other operand or not or check less than between two values, etc.
List of C++ relational operators:
Operator | Name | Example |
---|---|---|
== | Equal to. | A == B |
!= | Not equal. | A != B |
> | Greater than. | A > B |
< | Less than. | A < B |
>= | Greater than or equal to. | A >= B |
<= | Less than or equal to. | A <= B |
Example: C++ program for Relational Operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> using namespace std; int main() { int a, b; a = 3; b = 5; bool result; if (a == b) // false cout << "a == b is true" << endl; else cout << "a == b is false" << endl; if (a != b) // true cout << "a != b is true" << endl; else cout << "a != b is false" << endl; if (a > b) // false cout << "a > b is true" << endl; else cout << "a > b is false" << endl; if (a < b) // true cout << "a < b is true" << endl; else cout << "a < b is false" << endl; if (a >= b) // false cout << "a >= b is true" << endl; else cout << "a >= b is false" << endl; if (a <= b) // true cout << "a <= b is true" << endl; else cout << "a <= b is false" << endl; return 0; } |
Output:
1 2 3 4 5 6 | a == b is false a != b is true a > b is false a < b is true a >= b is false a <= b is true |