C++ Comparison Operators

πŸ” C++ Comparison Operators

Comparison (Relational) Operators ka use do values ko compare karne ke liye hota hai.
Inka result hamesha true (1) ya false (0) hota hai.


πŸ”Ή 1. List of Comparison Operators

OperatorNameExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

πŸ”Ή 2. Equal to (==)

int a = 10, b = 10;
cout << (a == b);

Output: 1


πŸ”Ή 3. Not Equal to (!=)

int a = 10, b = 5;
cout << (a != b);

Output: 1


πŸ”Ή 4. Greater / Less Than

cout << (10 > 5); // 1
cout << (10 < 5); // 0

πŸ”Ή 5. Greater or Equal / Less or Equal

cout << (10 >= 10); // 1
cout << (5 <= 3); // 0

πŸ”Ή 6. Comparison with Variables

int age = 18;

if (age >= 18) {
cout << "Eligible to vote";
}


πŸ”Ή 7. Comparison with boolalpha

cout << boolalpha << (5 == 3);

Output:

false

πŸ”Ή 8. String Comparison

string a = "apple";
string b = "apple";

if (a == b) {
cout << "Same";
}


❌ Common Mistakes

if (a = b) // ❌ assignment operator

βœ” Correct:

if (a == b)

πŸ“Œ Summary

  • Comparison operators result true/false

  • Mostly used in if, while, for

  • == and = ka difference samajhna zaroori

You may also like...