Relational operators in C++ are used to compare values. They evaluate to a boolean value, either true or false, depending on the relationship between the operands.
These operators are essential for making decisions in conditional statements and loops.
List of Relational Operators in C++
Operator | Name | Description | Example |
---|---|---|---|
== | Equal to | Returns true if operands are equal | a == b |
!= | Not equal to | Returns true if operands are not equal | a != b |
> | Greater than | Returns true if left operand is greater | a > b |
< | Less than | Returns true if left operand is smaller | a < b |
>= | Greater than or equal | Returns true if left operand is greater/equal | a >= b |
<= | Less than or equal | Returns true if left operand is smaller/equal | a <= b |
1. Equal to Operator (==)
The == operator checks if two values are equal. It returns true if they are equal and false otherwise.
#include <iostream> using namespace std; int main() { int a = 5, b = 5, c = 10; cout << "Is a equal to b? " << (a == b) << endl; // Output: 1 (true) cout << "Is a equal to c? " << (a == c) << endl; // Output: 0 (false) return 0; }
Explanation:
- a == b returns true because both have the value 5.
- a == c returns false because a and c have different values.
Output:
Is a equal to b? 1 Is a equal to c? 0
2. Not Equal to Operator (!=)
The != operator checks if two values are not equal. It returns true if they are not equal and false otherwise.
#include <iostream> using namespace std; int main() { int x = 5, y = 10; cout << "Is x not equal to y? " << (x != y) << endl; // Output: 1 (true) cout << "Is x not equal to 5? " << (x != 5) << endl; // Output: 0 (false) return 0; }
Explanation:
- x != y returns true because 5 and 10 are different.
- x != 5 returns false because x is equal to 5.
Output:
Is x not equal to y? 1 Is x not equal to 5? 0
3. Greater Than Operator (>)
The > operator checks if the left operand is greater than the right operand.
#include <iostream> using namespace std; int main() { int a = 10, b = 5; cout << "Is a greater than b? " << (a > b) << endl; // Output: 1 (true) cout << "Is b greater than a? " << (b > a) << endl; // Output: 0 (false) return 0; }
Explanation:
- a > b returns true because 10 is greater than 5.
- b > a returns false because 5 is not greater than 10.
Output:
Is a greater than b? 1 Is b greater than a? 0
4. Less Than Operator (<)
The < operator checks if the left operand is less than the right operand.
#include <iostream> using namespace std; int main() { int x = 3, y = 8; cout << "Is x less than y? " << (x < y) << endl; // Output: 1 (true) cout << "Is y less than x? " << (y < x) << endl; // Output: 0 (false) return 0; }
Explanation:
- x < y returns true because 3 is less than 8.
- y < x returns false because 8 is not less than 3.
Output:
Is x less than y? 1 Is y less than x? 0
5. Greater Than or Equal To Operator (>=)
The >= operator checks if the left operand is greater than or equal to the right operand.
#include <iostream> using namespace std; int main() { int a = 5, b = 5, c = 3; cout << "Is a greater than or equal to b? " << (a >= b) << endl; // Output: 1 (true) cout << "Is a greater than or equal to c? " << (a >= c) << endl; // Output: 1 (true) cout << "Is c greater than or equal to b? " << (c >= b) << endl; // Output: 0 (false) return 0; }
Explanation:
- a >= b returns true because a is equal to b.
- a >= c returns true because 5 is greater than 3.
- c >= b returns false because 3 is less than 5.
Output:
Is a greater than or equal to b? 1 Is a greater than or equal to c? 1 Is c greater than or equal to b? 0
6. Less Than or Equal To Operator (<=)
The <= operator checks if the left operand is less than or equal to the right operand.
#include <iostream> using namespace std; int main() { int x = 5, y = 10, z = 5; cout << "Is x less than or equal to y? " << (x <= y) << endl; // Output: 1 (true) cout << "Is x less than or equal to z? " << (x <= z) << endl; // Output: 1 (true) cout << "Is y less than or equal to x? " << (y <= x) << endl; // Output: 0 (false) return 0; }
Explanation:
- x <= y returns true because 5 is less than 10.
- x <= z returns true because x is equal to z.
- y <= x returns false because 10 is greater than 5.
Output:
Is x less than or equal to y? 1 Is x less than or equal to z? 1 Is y less than or equal to x? 0
7. Using Relational Operators in Conditional Statements
Relational operators are commonly used in if statements to make decisions based on comparisons.
#include <iostream> using namespace std; int main() { int age = 20; if (age >= 18) { cout << "You are an adult." << endl; } else { cout << "You are a minor." << endl; } return 0; }
Explanation:
- if (age >= 18) checks if age is 18 or more, printing “You are an adult” if true.
Output:
You are an adult.
8. Using Relational Operators in Loops
Relational operators can be used in loops, such as for and while, to control the loop's execution.
#include <iostream> using namespace std; int main() { int n = 5; cout << "Counting down from " << n << ":" << endl; while (n > 0) { cout << n << " "; n--; } cout << "Lift off!" << endl; return 0; }
Explanation:
- The while loop uses n > 0 as the condition, which runs until n becomes 0.
Output:
Counting down from 5: 5 4 3 2 1 Lift off!
9. Combining Relational Operators with Logical Operators
Relational operators can be combined with logical operators (&&, ||, !) to create more complex conditions.
#include <iostream> using namespace std; int main() { int score = 85; if (score >= 90) { cout << "Grade: A" << endl; } else if (score >= 75 && score < 90) { cout << "Grade: B" << endl; } else if (score >= 60 && score < 75) { cout << "Grade: C" << endl; } else { cout << "Grade: F" << endl; } return 0; }
Explanation:
- Multiple relational and logical operators are used to evaluate the score and assign a grade.
Output:
Grade: B
10. Summary Table of Relational Operators in C++
Operator | Description | Example | Explanation |
---|---|---|---|
== | Equal to | a == b | Returns true if a equals b |
!= | Not equal to | a != b | Returns true if a is not b |
> | Greater than | a > b | Returns true if a is greater |
< | Less than | a < b | Returns true if a is smaller |
>= | Greater than or equal to | a >= b | Returns true if a is >= b |
<= | Less than or equal to | a <= b | Returns true if a is <= b |
Complete Example: Using Relational Operators for a Simple Age Verification Program
This program uses relational operators to check if a person is eligible for certain age-based milestones.
#include <iostream> using namespace std; int main() { int age; cout << "Enter your age: "; cin >> age; if (age >= 18) { cout << "You are eligible to vote." << endl; } if (age >= 16) { cout << "You are eligible for a driving license." << endl; } if (age >= 65) { cout << "You are eligible for senior benefits." << endl; } else { cout << "You are not eligible for senior benefits." << endl; } return 0; }
Explanation:
- The program takes age as input and checks eligibility for voting, driving, and senior benefits using relational operators.
Sample Output:
Enter your age: 70 You are eligible to vote. You are eligible for a driving license. You are eligible for senior benefits.
Relational operators in C++ are crucial for making comparisons and controlling program flow based on conditions.