Comparison Operators

Comparison operators in C++ are used to compare two values or variables. They return a boolean result:

  • true (1)
  • false (0)

These operators are mainly used in conditions and decision-making statements.

What are Comparison Operators?

Comparison operators check relationships between values such as:

  • Equal to
  • Greater than
  • Less than

They help programs make decisions based on conditions.

Types of Comparison Operators in C++

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Equal To Operator (==)

Checks if two values are equal.

Example

#include <iostream>
using namespace std;

int main() {
int a = 10;
int b = 10;

cout << (a == b);

return 0;
}

Output

1

Not Equal To Operator (!=)

Checks if two values are different.

Example

cout << (10 != 5);

Greater Than Operator (>)

Checks if the left value is greater.

Example

cout << (10 > 5);

Less Than Operator (<)

Checks if the left value is smaller.

Example

cout << (5 < 10);

Greater Than or Equal To (>=)

Checks if value is greater or equal.

Example

cout << (10 >= 10);

Less Than or Equal To (<=)

Checks if value is smaller or equal.

Example

cout << (5 <= 10);

Complete Example Program

#include <iostream>
using namespace std;

int main() {
int a = 10;
int b = 5;

cout << (a == b) << endl;
cout << (a != b) << endl;
cout << (a > b) << endl;
cout << (a < b) << endl;
cout << (a >= b) << endl;
cout << (a <= b) << endl;

return 0;
}

Output

0
1
1
0
1
0

Using Comparison Operators in Conditions

if (age >= 18) {
cout << "Adult";
}

Important Points

  • Comparison operators return boolean values
  • true is displayed as 1
  • false is displayed as 0
  • They are commonly used in if, while, and loops

Why Comparison Operators are Important

They are important because they:

  • Help in decision-making
  • Control program flow
  • Compare values efficiently
  • Are used in conditions and loops

Real-Life Example

Comparison operators are used in:

  • Login systems
  • Age verification
  • Exam result checking
  • Banking conditions
  • Game logic

Conclusion

Comparison operators in C++ are essential for checking conditions and making decisions in programs. They compare values and return true or false results, helping create logical and interactive applications.

Home » C++ Fundamentals (Beginner Level) > Operators and Conditions > Comparison Operators