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, less, greater/equal, or less/equal.

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

Comparison operators in C++ are:

  • == (Equality): Returns true if two values are equal.
  • != (Inequality): Returns true if two values are not equal.
  • > (Greater than): Returns true if the value on the left is greater than the value on the right.
  • < (Less than): Returns true if the value on the left is less than the value on the right.
  • >= (Greater than or equal to): Returns true if the value on the left is greater than or equal to the value on the right.
  • <= (Less than or equal to): Returns true if the value on the left is less than or equal to the value on the right.

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.

For example:

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 a 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 a value is less than another.

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

Greater than or Equal to (>=)

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

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

Less than or Equal to (<=)

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

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

Usage Examples

Variable Comparison

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

int age = 20;

if (age >= 18) {
    std::cout << "You are of legal age." << std::endl;
} else {
    std::cout << "You are under age." << 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

Comparison operators are used to compare two values and determine if a statement is true or false.

In C++, the results of these comparisons are boolean values, i.e., true or false.

List of Comparison Operators in C++

OperatorDescriptionExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Usage of Comparison Operators

Equality and Inequality

The == and != operators are used to compare if two values are equal or different respectively. Here’s an example:

int a = 5;
int b = 3;

bool equal = a == b; // equal will contain the value false
bool different = a != b; // different will contain the value true

Numeric Relations

The <, >, <=, and >= operators are used to compare numeric relations between values. For example:

int x = 10;
int y = 8;

bool less = x < y; // less will contain the value false
bool greater = x > y; // greater will contain the value true
bool lessOrEqual = x <= y; // lessOrEqual will contain the value false
bool greaterOrEqual = x >= y; // greaterOrEqual will contain the value true

Usage Example

Suppose you are developing a program that checks if a user is eligible to vote based on their age. Here’s an example of how you could use comparison operators in C++:

#include <iostream>
using namespace std;

int main() {
    int userAge;

    cout << "Enter your age: ";
    cin >> userAge;

    if (userAge >= 18) {
        cout << "You are eligible to vote!" << endl;
    } else {
        cout << "You are not eligible to vote." << endl;
    }

    return 0;
}

In this example, we use the >= operator to compare the age entered by the user with the value 18. If the age is greater than or equal to 18, the program displays a message indicating that the user is eligible to vote; otherwise, it displays a message indicating the opposite.

Spaceship Operator <=> ⋆

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

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

(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