C++ Logical Operators
Last Updated :
31 Jul, 2023
In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false).
In C++, there are three logical operators:
- Logical AND ( && ) Operator
- Logical OR ( || ) Operator
- Logical NOT ( ! ) Operator
Let’s discuss each of the operators in detail.
1. Logical AND Operator ( && )
The C++ logical AND operator (&&) is a binary operator that returns true if both of its operands are true. Otherwise, it returns false. Here’s the truth table for the AND operator:
true
|
true
|
true
|
true
|
false
|
false
|
false
|
true
|
false
|
false
|
false
|
false
|
Note: In C, false is represented by 0 while the true is represented as any non-zero value, generally 1.
Syntax of Logical AND
expression1 && expression2
Example of Logical AND in C++
C++
#include <iostream>
using namespace std;
int main()
{
int age = 25;
bool isStudent = true ;
if (age > 18 && isStudent) {
cout << "You are eligible for a student discount."
<< endl;
}
else {
cout << "You are not eligible for a student "
"discount."
<< endl;
return 0;
}
|
Output
You are eligible for a student discount.
Explanation: In the above code, we have used AND operator in the if condition to check whether the age is greater than 18 and the person is a check. If both conditions are true, the message “You are eligible for a student discount.” will be printed. Otherwise, the else statement is executed.
2. Logical OR Operator ( || )
The C++ logical OR operator ( || ) is a binary operator that returns true if at least one of its operands is true. It returns false only when both operands are false. Here’s the truth table for the OR operator:
true
|
true
|
true
|
true
|
false
|
true
|
false
|
true
|
true
|
false
|
false
|
false
|
Syntax of Logical OR
expression1 || expression2
Example of Logical OR in C++
C++
#include <iostream>
using namespace std;
int main()
{
int num = 7;
if (num <= 0 || num >= 10) {
cout
<< "The number is outside the range of 0 to 10."
<< endl;
}
else {
cout << "The number is between 0 to 10." << endl;
}
return 0;
}
|
Output
The number is between 0 to 10.
Explanation: In the above code, the condition num < 0 || num > 10 checks whether the number is either less than equal to 0 or greater than equal to 10. If either of these conditions is true, the message “The number is outside the range of 0 to 10.” will be printed otherwise else statement is printed.
3. Logical NOT Operator ( ! )
The C++ logical NOT operator ( ! ) is a unary operator that is used to negate the value of a condition. It returns true if the condition is false, and false if the condition is true. Here’s the truth table for the NOT operator:
Syntax of Logical NOT
! expression
Example of Logical NOT in C++
C++
#include <iostream>
using namespace std;
int main()
{
bool isLoggedIn = false ;
if (!isLoggedIn) {
cout << "Please log in to access this feature."
<< endl;
}
else {
cout << "Welcome to GeeksforGeeks!" << endl;
}
return 0;
}
|
Output
Please log in to access this feature.
Explanation: In the above code, the condition ‘!isLoggedIn’ checks whether the user is not logged in. If the condition is true (i.e., the user is not logged in), the message “Please log in to access this feature.” will be displayed otherwise else statement will be printed.
Conclusion
Logical operators in C++ enable you to combine conditions and create expressive logic. They are invaluable when working with decision-making and branching in your code. By mastering these logical operators, you can write more robust and flexible programs.
Share your thoughts in the comments
Please Login to comment...