How to Use Comparison Operators in C++: Syntax and Examples
In C++,
comparison operators are used to compare two values and return a boolean result. Common operators include == (equal), != (not equal), < (less than), > (greater than), <= (less than or equal), and >= (greater than or equal). These operators help control program flow by making decisions based on comparisons.Syntax
Comparison operators in C++ compare two values and return true or false. Here are the common operators:
==: Checks if two values are equal.!=: Checks if two values are not equal.<: Checks if the left value is less than the right value.>: Checks if the left value is greater than the right value.<=: Checks if the left value is less than or equal to the right value.>=: Checks if the left value is greater than or equal to the right value.
These operators are used in expressions like a < b or x == y.
cpp
a == b // true if a equals b a != b // true if a does not equal b a < b // true if a is less than b a > b // true if a is greater than b a <= b // true if a is less than or equal to b a >= b // true if a is greater than or equal to b
Example
This example shows how to use comparison operators to compare two numbers and print the results.
cpp
#include <iostream> int main() { int x = 10; int y = 20; std::cout << "x == y: " << (x == y) << "\n"; std::cout << "x != y: " << (x != y) << "\n"; std::cout << "x < y: " << (x < y) << "\n"; std::cout << "x > y: " << (x > y) << "\n"; std::cout << "x <= y: " << (x <= y) << "\n"; std::cout << "x >= y: " << (x >= y) << "\n"; return 0; }
Output
x == y: 0
x != y: 1
x < y: 1
x > y: 0
x <= y: 1
x >= y: 0
Common Pitfalls
One common mistake is using = (assignment) instead of == (comparison). This causes the variable to be assigned a value instead of checking equality, which can lead to bugs.
Another pitfall is comparing floating-point numbers directly due to precision issues; instead, check if their difference is very small.
cpp
#include <iostream> int main() { int a = 5; // Wrong: assignment instead of comparison if (a = 3) { // This assigns 3 to a, always true if 3 is non-zero std::cout << "This runs even if a was not 3.\n"; } // Correct: comparison if (a == 3) { std::cout << "a is 3.\n"; } else { std::cout << "a is not 3.\n"; } return 0; }
Output
This runs even if a was not 3.
a is 3.
Quick Reference
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| < | Less than | 3 < 5 | true |
| > | Greater than | 7 > 2 | true |
| <= | Less than or equal to | 4 <= 4 | true |
| >= | Greater than or equal to | 6 >= 8 | false |
Key Takeaways
Use comparison operators like ==, !=, <, >, <=, >= to compare values in C++.
Always use == for comparison, not = which is for assignment.
Comparison operators return true or false, useful in conditions and loops.
Be careful comparing floating-point numbers directly due to precision.
Use parentheses to ensure correct evaluation order in complex expressions.