Language: EN

cpp-operadores-comparacion

Comparison Operators in C++

Comparison operators allow us to compare two values and determine if they are equal, different, greater, lesser, or greater/equal and lesser/equal.

The results of comparisons are boolean values (true or false), and they are fundamental to control flow logic.

The comparison operators in C++ are:

OperatorNameDescription
==EqualityCompares if two values are equal
!=InequalityCompares if two values are different
>Greater thanChecks if one value is greater than another
<Less thanChecks if one value is less than another
>=Greater or equal toChecks if one value is greater or equal
<=Less or equal toChecks if one value is less or equal

These operators are fundamental for decision-making in programs and are commonly used in control structures such as conditionals or loops.

List of comparison operators

Equality (==)

The equality operator (==) is used to check if two values are equal. It compares two values and returns true if they are equal, or false otherwise.

For example:

int a = 5;
int b = 5;
bool isEqual = (a == b); // true, since a and b are equal

Inequality (!=)

The inequality operator (!=) is used to check if two values are not equal. It compares two values and returns true if they are different, or false otherwise.

int a = 5;
int b = 3;
bool isNotEqual = (a != b); // true, since a is not equal to b

Greater than (>)

The greater than operator (>) is used to check if one value is greater than another.

int a = 10;
int b = 5;
bool isGreater = (a > b); // true, since a is greater than b

Less than (<)

The less than operator (<) is used to check if one value is less than another.

int a = 3;
int b = 7;
bool isLess = (a < b); // true, since a is less than b

Greater or equal to (>=)

The greater or equal to operator (>=) is used to check if one value is greater than or equal to another.

int a = 10;
int b = 10;
bool isGreaterEqual = (a >= b); // true, since a is equal to b

Less or equal to (<=)

The less or equal to operator (<=) is used to check if one value is less than or equal to another.

int a = 5;
int b = 7;
bool isLessEqual = (a <= b); // true, since a is less than b

Spaceship Operator <=>

C++20 provides the three-way comparison operator <=>, also called the spaceship operator, which allows comparing two objects similarly to strcmp.

The operator returns an object that can be directly compared with a positive, 0, or negative integer.

(3 <=> 3) == 0; // true
(3 <=> 5) == 0; // false 
('a' <=> 'a') == 0; // true 

(3 <=> 5) > 0; // false
(3 <=> 5) < 0; // true

(7 <=> 5) > 0; // true
(7 <=> 5) < 0; // false

Usage examples

Variable comparison

Comparison operators are commonly used in control structures to make decisions based on comparisons between variables.

int age = 20;

if (age >= 18) {
    std::cout << "You are an adult." << std::endl;
} else {
    std::cout << "You are a minor." << std::endl;
}

Use in loops

Comparison operators are also used in loops to control the number of iterations.

for (int i = 0; i < 10; ++i) {
    std::cout << "Number: " << i << std::endl;
}

Complex comparisons

It is possible to combine multiple comparison operators to create more complex conditions.

int a = 5;
int b = 10;
int c = 5;

bool result = (a < b) && (a == c); // true, since a is less than b and a is equal to c