C++ Relational and Logical Operators
In C++, relational operators and logical operators are used for comparison and logical operations, respectively. These operators are fundamental in decision-making processes, such as controlling the flow of execution using conditional statements (like if
and while
). Relational operators compare values, while logical operators help evaluate multiple conditions.
In this blog post, we'll cover:
Let’s dive into both types of operators and see how they work.
Relational operators are used to compare two values. These operators return a boolean value: true
or false
.
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
variable1 relational_operator variable2;
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
// Equal to
cout << "a == b: " << (a == b) << endl; // False (0)
// Not equal to
cout << "a != b: " << (a != b) << endl; // True (1)
// Greater than
cout << "a > b: " << (a > b) << endl; // True (1)
// Less than
cout << "a < b: " << (a < b) << endl; // False (0)
// Greater than or equal to
cout << "a >= b: " << (a >= b) << endl; // True (1)
// Less than or equal to
cout << "a <= b: " << (a <= b) << endl; // False (0)
return 0;
}
Output:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0
Explanation:
==
checks if two values are equal.!=
checks if two values are not equal.>
checks if the left operand is greater than the right operand.<
checks if the left operand is less than the right operand.>=
checks if the left operand is greater than or equal to the right operand.<=
checks if the left operand is less than or equal to the right operand.These operators are primarily used in conditional statements like if
, while
, and loops to make decisions based on comparisons.
Logical operators are used to combine multiple conditions, allowing you to evaluate more complex boolean expressions. These operators return true
or false
based on the conditions being evaluated.
&&
: Logical AND||
: Logical OR!
: Logical NOT
condition1 logical_operator condition2;
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
bool condition1 = (a > b); // True, because 10 is greater than 5
bool condition2 = (a == b); // False, because 10 is not equal to 5
// Logical AND
cout << "(a > b) && (a == b): " << (condition1 && condition2) << endl; // False (0)
// Logical OR
cout << "(a > b) || (a == b): " << (condition1 || condition2) << endl; // True (1)
// Logical NOT
cout << "!(a > b): " << !(a > b) << endl; // False (0)
return 0;
}
Output:
(a > b) && (a == b): 0
(a > b) || (a == b): 1
!(a > b): 0
Explanation:
&&
(AND): Returns true
only if both conditions are true.||
(OR): Returns true
if at least one of the conditions is true.!
(NOT): Returns the opposite of the given condition (negates the result).You can combine relational operators with logical operators to create more complex conditions.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5, c = 20;
// Checking if a is greater than b and less than c
if ((a > b) && (a < c)) {
cout << "a is between b and c" << endl; // This will be true
}
// Checking if a is not equal to b or greater than c
if ((a != b) || (a > c)) {
cout << "a is either not equal to b or greater than c" << endl; // This will be true
}
// Negating the condition
if (!(a > b)) {
cout << "a is not greater than b" << endl; // This will not execute, as a > b is true
}
return 0;
}
Output:
a is between b and c
a is either not equal to b or greater than c
Explanation:
if
statement checks if a > b
and a < c
are both true using &&
.if
statement checks if a != b
or a > c
is true using ||
.if
statement negates the result of a > b
using !
.These operators are essential in decision-making processes. Here's how you might use them in real-world applications:
#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;
} else {
cout << "You are not eligible to vote." << endl;
}
return 0;
}
In this example, the relational operator >=
checks if the user’s age is greater than or equal to 18.
#include <iostream>
using namespace std;
int main() {
int age, years_of_experience;
cout << "Enter your age: ";
cin >> age;
cout << "Enter years of experience: ";
cin >> years_of_experience;
// Check if the person is eligible for a senior position
if ((age >= 30) && (years_of_experience >= 5)) {
cout << "You are eligible for a senior position." << endl;
} else {
cout << "You are not eligible for a senior position." << endl;
}
return 0;
}
In this example, the &&
(AND) operator combines two conditions to check if both the age is greater than or equal to 30 and the years of experience is greater than or equal to 5.