100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Relational Operators in C++

Understand C++ relational (comparison) operators such as ==, !=, <, >, <=, and >=, and how they always evaluate to a bool result.

OperatorsBeginner5 min readJul 7, 2026
Analogies

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

cpp
operand1 == operand2
operand1 != operand2
operand1 < operand2
operand1 > operand2
operand1 <= operand2
operand1 >= operand2

3. 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

cpp
#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

text
a == b: false
a != b: true
a < b: true
a >= b: false

6. Key Takeaways

  • Relational operators always produce a bool result: true or false.
  • The six operators are ==, !=, <, >, <=, >=.
  • == compares values; = assigns a value — mixing these up is a classic bug.
  • std::string and other overloading types support relational operators for lexicographic comparison.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#RelationalOperatorsInC#Relational#Operators#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep