Introduction
Logical operators in C++ are used to perform logical operations on boolean values. They are essential for controlling the flow of your program using conditions, combining multiple conditions, and making decisions based on these conditions. Logical operators return a boolean value (true or false) based on the logic they perform.
List of Logical Operators
- Logical AND (
&&): Returnstrueif both operands aretrue. - Logical OR (
||): Returnstrueif at least one of the operands istrue. - Logical NOT (
!): Returnstrueif the operand isfalse, andfalseif the operand istrue.
Logical Operators with Examples
Logical AND (&&)
The logical AND operator returns true if both operands are true. If either operand is false, it returns false.
Example
#include <iostream>
using namespace std;
int main() {
bool a = true;
bool b = false;
bool result = a && b; // Checks if both a and b are true
cout << "a && b: " << result << endl; // Output the result
return 0;
}
Output
a && b: 0
Logical OR (||)
The logical OR operator returns true if at least one of the operands is true. If both operands are false, it returns false.
Example
#include <iostream>
using namespace std;
int main() {
bool a = true;
bool b = false;
bool result = a || b; // Checks if at least one of a or b is true
cout << "a || b: " << result << endl; // Output the result
return 0;
}
Output
a || b: 1
Logical NOT (!)
The logical NOT operator returns true if the operand is false, and false if the operand is true.
Example
#include <iostream>
using namespace std;
int main() {
bool a = true;
bool result = !a; // Checks if a is false
cout << "!a: " << result << endl; // Output the result
return 0;
}
Output
!a: 0
Combining Logical Operators
Logical operators can be combined to create complex logical expressions.
Example
#include <iostream>
using namespace std;
int main() {
bool a = true;
bool b = false;
bool c = true;
// Combining logical operators
bool result = (a && b) || (b && c) || (a && c);
cout << "(a && b) || (b && c) || (a && c): " << result << endl; // Output the result
return 0;
}
Output
(a && b) || (b && c) || (a && c): 1
Explanation
-
Logical AND (
&&)- Returns
trueif both operands aretrue. - Example:
a && breturnsfalsebecausebisfalse.
- Returns
-
Logical OR (
||)- Returns
trueif at least one of the operands istrue. - Example:
a || breturnstruebecauseaistrue.
- Returns
-
Logical NOT (
!)- Returns
trueif the operand isfalse, andfalseif the operand istrue. - Example:
!areturnsfalsebecauseaistrue.
- Returns
-
Combining Logical Operators
(a && b) || (b && c) || (a && c)combines multiple logical operators to form a complex expression.- This expression evaluates to
truebecause(a && c)istrue.
Conclusion
Logical operators are essential tools in C++ for performing logical operations on boolean values. This chapter covered the logical AND (&&), logical OR (||), and logical NOT (!) operators with simple examples and their outputs. Understanding these operators will help you create complex conditional logic and control the flow of your programs more effectively. In the next chapter, we will explore bitwise operators in C++.