C++ Relational Operators
Relational operators in C++ are used to compare two values or expressions and return a boolean result, either true
or false
. They are essential for control flow in programs, especially in conditional statements and loops. Here’s a detailed explanation of the different relational operators available in C++:
1. Equal to Operator (==
)
This operator checks if two operands are equal.
Syntax:
operand1 == operand2
Example:
#include <iostream> int main() { int a = 5, b = 5; bool isEqual = (a == b); // isEqual is true std::cout << "a is equal to b: " << std::boolalpha << isEqual << std::endl; return 0; }
2. Not Equal to Operator (!=
)
This operator checks if two operands are not equal.
Syntax:
operand1 != operand2
Example:
#include <iostream> int main() { int a = 5, b = 3; bool isNotEqual = (a != b); // isNotEqual is true std::cout << "a is not equal to b: " << std::boolalpha << isNotEqual << std::endl; return 0; }
3. Greater than Operator (>
)
This operator checks if the left operand is greater than the right operand.
Syntax:
operand1 > operand2
Example:
#include <iostream> int main() { int a = 7, b = 5; bool isGreater = (a > b); // isGreater is true std::cout << "a is greater than b: " << std::boolalpha << isGreater << std::endl; return 0; }
4. Less than Operator (<
)
This operator checks if the left operand is less than the right operand.
Syntax:
operand1 < operand2
Example:
#include <iostream> int main() { int a = 4, b = 6; bool isLess = (a < b); // isLess is true std::cout << "a is less than b: " << std::boolalpha << isLess << std::endl; return 0; }
5. Greater than or Equal to Operator (>=
)
This operator checks if the left operand is greater than or equal to the right operand.
Syntax:
operand1 >= operand2
Example:
#include <iostream> int main() { int a = 5, b = 5; bool isGreaterOrEqual = (a >= b); // isGreaterOrEqual is true std::cout << "a is greater than or equal to b: " << std::boolalpha << isGreaterOrEqual << std::endl; return 0; }
6. Less than or Equal to Operator (<=
)
This operator checks if the left operand is less than or equal to the right operand.
Syntax:
operand1 <= operand2
Example:
#include <iostream> int main() { int a = 5, b = 7; bool isLessOrEqual = (a <= b); // isLessOrEqual is true std::cout << "a is less than or equal to b: " << std::boolalpha << isLessOrEqual << std::endl; return 0; }
Usage of Relational Operators
Relational operators are typically used in conditional statements (like if
, while
, and for
) to control the flow of the program based on comparisons.
Example: Using Relational Operators in Conditional Statements
Here’s an example that uses relational operators to determine if a number is within a specific range.
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
if (number >= 10 && number <= 20) {
std::cout << "The number is between 10 and 20." << std::endl;
} else {
std::cout << "The number is outside the range." << std::endl;
}
return 0;
}
Important Notes
- Data Types: Relational operators can be used with various data types, including integers, floating-point numbers, and characters.
- Boolean Results: The result of a relational operation is always a boolean value (
true
orfalse
). - Operator Precedence: Relational operators have lower precedence than arithmetic operators. This means arithmetic operations are evaluated first unless parentheses are used.