C++ Comparison Operators
Comparison operators in C++ are used to compare two values or expressions. They return a boolean value (true or false).
List of Comparison Operators
| Operator | Description | Example |
|---|---|---|
| == | Equal to | 5 == 5 // true |
| != | Not equal to | 5 != 3 // true |
| > | Greater than | 7 > 5 // true |
| < | Less than | 3 < 7 // true |
| >= | Greater than or equal to | 5 >= 5 // true |
| <= | Less than or equal to | 4 <= 5 // true |
Example Program
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 5;
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: 1 a < b: 0 a >= b: 1 a <= b: 0
Important Notes
- Comparison operators always return true (1) or false (0).
- They are commonly used in if statements, loops, and conditional expressions.
- Parentheses are often used to ensure correct evaluation of expressions.
Next Topic
Next, learn about C++ Logical Operators.