Introduction
Relational operators in C++ are used to compare two values. These operators determine the relationship between two operands and return a boolean value (true
or false
). Understanding relational operators is crucial for controlling the flow of your program using conditions and loops.
List of Relational Operators
- Equal to (
==
): Checks if two operands are equal. - Not equal to (
!=
): Checks if two operands 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.
Relational Operators with Examples
Equal to (==
)
The ==
operator checks if two operands are equal. It returns true
if they are equal, otherwise false
.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 5;
bool result = (a == b); // Checks if a is equal to b
cout << "a == b: " << result << endl; // Output the result
return 0;
}
Output
a == b: 1
Not equal to (!=
)
The !=
operator checks if two operands are not equal. It returns true
if they are not equal, otherwise false
.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 3;
bool result = (a != b); // Checks if a is not equal to b
cout << "a != b: " << result << endl; // Output the result
return 0;
}
Output
a != b: 1
Greater than (>
)
The >
operator checks if the left operand is greater than the right operand. It returns true
if the left operand is greater, otherwise false
.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 3;
bool result = (a > b); // Checks if a is greater than b
cout << "a > b: " << result << endl; // Output the result
return 0;
}
Output
a > b: 1
Less than (<
)
The <
operator checks if the left operand is less than the right operand. It returns true
if the left operand is less, otherwise false
.
Example
#include <iostream>
using namespace std;
int main() {
int a = 3;
int b = 5;
bool result = (a < b); // Checks if a is less than b
cout << "a < b: " << result << endl; // Output the result
return 0;
}
Output
a < b: 1
Greater than or equal to (>=
)
The >=
operator checks if the left operand is greater than or equal to the right operand. It returns true
if the left operand is greater or equal, otherwise false
.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 5;
bool result = (a >= b); // Checks if a is greater than or equal to b
cout << "a >= b: " << result << endl; // Output the result
return 0;
}
Output
a >= b: 1
Less than or equal to (<=
)
The <=
operator checks if the left operand is less than or equal to the right operand. It returns true
if the left operand is less or equal, otherwise false
.
Example
#include <iostream>
using namespace std;
int main() {
int a = 3;
int b = 5;
bool result = (a <= b); // Checks if a is less than or equal to b
cout << "a <= b: " << result << endl; // Output the result
return 0;
}
Output
a <= b: 1
Conclusion
Relational operators are essential tools in C++ for comparing values and controlling the flow of your program using conditional statements. This chapter covered the equal to (==
), not equal to (!=
), greater than (>
), less than (<
), greater than or equal to (>=
), and less than or equal to (<=
) operators with simple examples and their outputs. Understanding these operators will help you write more effective and efficient conditional logic in your programs. In the next chapter, we will explore logical operators in C++.