C++ relational operators
Created By: chatGPT
C++ provides several relational operators that are used to compare values, which include:
==
, !=
, >
, <
, >=
, and <=
. These operators return a boolean value — true
or false
depending on the comparison. Here’s a breakdown of each operator: ==
checks if two values are equal.if (a == b) {
// true if a is equal to b
}
!=
checks if two values are not equal.if (a != b) {
// true if a is not equal to b
}
>
checks if the left value is greater than the right value.if (a > b) {
// true if a is greater than b
}
<
checks if the left value is less than the right value.if (a < b) {
// true if a is less than b
}
>=
checks if the left value is greater than or equal to the right value.if (a >= b) {
// true if a is greater than or equal to b
}
<=
checks if the left value is less than or equal to the right value.if (a <= b) {
// true if a is less than or equal to b
}
Example of using relational operators in a program: Below is a simple program that demonstrates the use of these operators.
#include <iostream>
int main() {
int a = 10;
int b = 20;
std::cout << "a == b: " << (a == b) << std::endl;
std::cout << "a != b: " << (a != b) << std::endl;
std::cout << "a > b: " << (a > b) << std::endl;
std::cout << "a < b: " << (a < b) << std::endl;
std::cout << "a >= b: " << (a >= b) << std::endl;
std::cout << "a <= b: " << (a <= b) << std::endl;
return 0;
}