C++ Logical Operators
Logical operators in C++ are used to perform logical operations on boolean expressions or values that can be evaluated as true or false. These operators are essential for creating complex conditional statements, controlling program flow, and implementing decision-making logic in applications. There are 3 logical operators in C++:
- Logical AND (
&&) - Logical OR (
||) - Logical NOT (
!)
Logical AND (&&)
The logical AND operator && returns true only when both operands are true. It uses short-circuit evaluation, meaning if the first operand is false, the second operand is not evaluated.
Syntax of &&
expression1 && expression2
Here:
expression1andexpression2are boolean expressions or values that evaluate to true or false
Example of Logical AND (&&) Operator
#include <iostream>using namespace std;int main() {int age = 25;bool hasLicense = true;// Check if person can driveif (age >= 18 && hasLicense) {cout << "Person can drive legally." << endl;} else {cout << "Person cannot drive legally." << endl;}return 0;}
The output of this code is:
Person can drive legally.
Logical OR (||)
The logical OR operator || returns true if at least one of the operands is true. It also uses short-circuit evaluation, meaning if the first operand is true, the second operand is not evaluated.
Syntax of ||
expression1 || expression2
Example of Logical OR (||) Operator
#include <iostream>using namespace std;int main() {int day = 6; // Saturdaybool isHoliday = false;// Check if it's a weekend or holidayif (day == 6 || day == 7 || isHoliday) {cout << "No work today!" << endl;} else {cout << "Work day." << endl;}return 0;}
The output of this code is:
No work today!
Logical NOT (!)
The logical NOT operator ! reverses the boolean value of its operand. If the operand is true, it returns false, and vice versa.
Syntax of !
!expression1
Example of Logical NOT (!) Operator
#include <iostream>using namespace std;int main() {bool isRaining = false;if (!isRaining) {cout << "Perfect weather for a walk!" << endl;} else {cout << "Better stay inside." << endl;}return 0;}
The output of this code is:
Perfect weather for a walk!
Codebyte Example
The following Codebyte demonstrates how logical operators are used in a real-world scenario such as verifying login credentials:
The following table summarizes the three logical operators in C++:
| Symbol | Name | Description | Implementation Example |
|---|---|---|---|
&& |
Logical AND | Returns true if both operands are true | a && b |
|| |
Logical OR | Returns true if at least one operand is true | a || b |
! |
Logical NOT | Returns the opposite boolean value of the operand | !a |
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours