C++ is a powerful and versatile programming language that provides a wide range of tools and features to developers. Among these tools are the relational operators, fundamental building blocks that allow programmers to compare values and make decisions based on those comparisons. In this article, we will explore the various relational operators in C++, including the modern addition of the three-way comparison operator.
What are Relational Operators?
Relational operators in C++ are used to compare two values, determining the relationship between them. These operators help in constructing conditional statements, making decisions within a program based on the outcomes of these comparisons.
Here are the six relational operators in C++:
- Equal to (==): Checks if two values are equal.
- Not equal to (!=): Checks if two values are not equal.
- Greater than (>): Checks if the left operand is greater than the right operand.
- Less than (<): Checks if the left operand is less than the right operand.
- Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
Understanding Each Relational Operator
Equal to (==)
The equal to operator (==) checks whether the values on both sides are equal. If they are, the expression evaluates to true; otherwise, it evaluates to false. Here’s an example:
#include <iostream>
int main() {
int num1 = 10;
int num2 = 5;
if (num1 == num2) {
std::cout << "The numbers are equal." << std::endl;
} else {
std::cout << "The numbers are not equal." << std::endl;
}
return 0;
}
In this example, since num1 is not equal to num2, the program outputs “The numbers are not equal.”
Not Equal To (!=)
The not equal to operator (!=) checks if the values on both sides are not equal. If they are not equal, the expression evaluates to true; otherwise, it evaluates to false. Consider the following example:
#include <iostream>
int main() {
int num1 = 10;
int num2 = 5;
if (num1 != num2) {
std::cout << "The numbers are not equal." << std::endl;
} else {
std::cout << "The numbers are equal." << std::endl;
}
return 0;
}
Here, the program outputs “The numbers are not equal” because num1 and num2 have different values.
Greater Than (>)
The greater than operator (>) checks if the value on the left is greater than the value on the right. If it is, the expression evaluates to true; otherwise, it evaluates to false. Consider the following example:
#include <iostream>
int main() {
int num1 = 10;
int num2 = 5;
if (num1 > num2) {
std::cout << "num1 is greater than num2." << std::endl;
} else {
std::cout << "num1 is not greater than num2." << std::endl;
}
return 0;
}
In this case, the program outputs “num1 is greater than num2.”
Less Than (<)
The less than operator (<) checks if the value on the left is less than the value on the right. If it is, the expression evaluates to true; otherwise, it evaluates to false. Consider the following example:
#include <iostream>
int main() {
int num1 = 10;
int num2 = 5;
if (num1 < num2) {
std::cout << "num1 is less than num2." << std::endl;
} else {
std::cout << "num1 is not less than num2." << std::endl;
}
return 0;
}
Here, the program outputs “num1 is not less than num2” since num1 is greater than num2.
Greater Than or Equal To (>=)
The greater than or equal to operator (>=) checks if the value on the left is greater than or equal to the value on the right. If it is, the expression evaluates to true; otherwise, it evaluates to false. Consider the following example:
#include <iostream>
int main() {
int num1 = 10;
int num2 = 10;
if (num1 >= num2) {
std::cout << "num1 is greater than or equal to num2." << std::endl;
} else {
std::cout << "num1 is not greater than or equal to num2." << std::endl;
}
return 0;
}
In this example, the program outputs “num1 is greater than or equal to num2” because num1 is equal to num2.
Less Than or Equal To (<=)
The less than or equal to operator (<=) checks if the value on the left is less than or equal to the value on the right. If it is, the expression evaluates to true; otherwise, it evaluates to false. Consider the following example:
#include <iostream>
int main() {
int num1 = 5;
int num2 = 10;
if (num1 <= num2) {
std::cout << "num1 is less than or equal to num2." << std::endl;
} else {
std::cout << "num1 is not less than or equal to num2." << std::endl;
}
return 0;
}
In this instance, the program outputs “num1 is less than or equal to num2” as num1 is less than num2.
The Three-Way Comparison Operator (<=>)
C++20 introduces the three-way comparison operator (<=>), also known as the spaceship operator. This operator simplifies the process of comparing values and provides a more consistent and concise syntax. The spaceship operator returns one of three values: negative, zero, or positive, indicating less than, equal to, or greater than, respectively.
#include <iostream>
int main() {
int num1 = 6;
int num2 = 12;
// Using the Three-way comparison operator
if (num1 <=> num2 < 0) {
std::cout << "num1 is less than num2." << std::endl;
} else if (num1 <=> num2 > 0) {
std::cout << "num1 is greater than num2." << std::endl;
} else {
std::cout << "num1 and num2 are equal." << std::endl;
}
return 0;
}
In this example, the Three-way comparison operator is used to compare num1 and num2. If the result is less than zero, it means num1 is less than num2, and the corresponding message is displayed. If the result is greater than zero, it means num1 is greater than num2, and the appropriate message is shown. If the result is zero, it indicates that num1 and num2 are equal.
Combining Relational Operators with Logical Operators
Relational operators can be combined with logical operators (&&, ||, !) to create more complex conditions. Let’s explore a few examples to understand how these combinations work.
Combining with Logical AND (&&)
The logical AND operator (&&) combines two conditions and returns true only if both conditions are true. Here’s an example:
#include <iostream>
int main() {
int num1 = 5;
int num2 = 10;
if (num1 > 0 && num2 < 15) {
std::cout << "Both conditions are true." << std::endl;
} else {
std::cout << "At least one condition is false." << std::endl;
}
return 0;
}
In this example, the program checks whether num1 is greater than 0 and num2 is less than 15. If both conditions are true, it prints a message stating that both conditions are true. Otherwise, it prints a message indicating that at least one condition is false.
Combining with Logical OR (||)
The logical OR operator (||) combines two conditions and returns true if at least one of the conditions is true. Consider the following example:
#include <iostream>
int main() {
int num1 = 8;
int num2 = 5;
if (num1 > 10 || num2 < 0) {
std::cout << "At least one condition is true." << std::endl;
} else {
std::cout << "Both conditions are false." << std::endl;
}
return 0;
}
In this case, the program checks whether num1 is greater than 10 or num2 is less than 0. If at least one of the conditions is true, it prints a message stating that at least one condition is true. Otherwise, it prints a message indicating that both conditions are false.
Combining with Logical NOT (!)
The logical NOT operator (!) negates the result of a condition. It returns true if the condition is false, and vice versa. Let’s see an example:
#include <iostream>
int main() {
int num1 = 8;
if (!(num1 < 5)) {
std::cout << "The condition is false, thanks to the logical NOT operator." << std::endl;
} else {
std::cout << "The condition is true without the logical NOT operator." << std::endl;
}
return 0;
}
In this example, the program uses the logical NOT operator to check whether num1 is not less than 5. If the condition is false (i.e., num1 is not less than 5), it prints a message stating that the condition is false, thanks to the logical NOT operator. Otherwise, it prints a message indicating that the condition is true without the logical NOT operator.
Conclusion
Relational operators are essential tools for making decisions in C++ programs. The introduction of the spaceship operator in C++20 further enhances the language’s expressiveness and conciseness, making three-way comparisons more straightforward and consistent.
Understanding how to use relational operators effectively opens the door to creating efficient and concise code, enabling developers to implement a wide range of algorithms and applications. Whether it’s sorting data, searching for elements, or implementing conditional logic, the power of relational operators is omnipresent in the world of C++ programming.
Related: