1. Intro
Relational operators, also called comparison operators, compare two values and produce a bool result: true (1) or false (0). C++ provides six relational operators: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to). They are the backbone of conditions in if statements, loops, and sorting logic.
Cricket analogy: Umpires deciding lbw appeals produce a simple true or false, just like ==, <, or > comparing two run totals to decide who's leading — these six comparisons are the backbone of every "is Team A ahead of Team B" decision.
2. Syntax
operand1 == operand2
operand1 != operand2
operand1 < operand2
operand1 > operand2
operand1 <= operand2
operand1 >= operand23. Explanation
Every relational expression evaluates to a bool, which is 1 when printed via cout for true and 0 for false (unless boolalpha is used). Relational operators work on numeric types and on types that overload them, such as std::string (which compares lexicographically). A very common beginner mistake is writing = (assignment) instead of == (comparison) inside a condition, which silently assigns a value rather than comparing — this compiles because an assignment expression itself has a value.
Cricket analogy: A scoreboard displaying 1 for "team A leads" and 0 otherwise mirrors how bool prints via cout; comparing player names lexicographically like std::string works for sorting a batting-order alphabetically, but a scorer typing runs = 50 instead of runs == 50 accidentally overwrites the real score instead of checking it.
if (a = 5) is legal C++ but almost certainly a bug — it assigns 5 to a and then evaluates the assignment's result (5, which is truthy), rather than comparing a to 5. Always double-check you used == for comparisons.
4. Example
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << boolalpha;
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;
}5. Output
a == b: false
a != b: true
a < b: true
a >= b: false6. Key Takeaways
- Relational operators always produce a
boolresult:trueorfalse. - The six operators are
==,!=,<,>,<=,>=. ==compares values;=assigns a value — mixing these up is a classic bug.std::stringand other overloading types support relational operators for lexicographic comparison.
Practice what you learned
1. What type does every relational operator expression evaluate to in C++?
2. What is the danger of writing `if (a = 5)` instead of `if (a == 5)`?
3. What does `cout << (5 != 5);` print by default (without `boolalpha`)?
4. Which of these correctly checks that `x` is strictly between 1 and 10?
5. How does `std::string` comparison with `<` work?
Was this page helpful?
You May Also Like
Logical Operators in C++
Master the C++ logical operators &&, ||, and !, and understand short-circuit evaluation and how it can skip side effects.
if-else Statement in C++
Understand how the C++ `if-else` statement provides a two-way branch, executing one block when the condition is true and another when false.
Assignment Operators in C++
Learn C++ assignment operators including =, +=, -=, *=, /=, and %=, and how compound assignment shortens common update expressions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics