Logical operators help you combine or change true/false conditions in your program. They let you make decisions based on multiple checks.
Logical operators in C++
&& // Logical AND || // Logical OR ! // Logical NOT
AND (&&) is true only if both sides are true.
OR (||) is true if at least one side is true.
NOT (!) flips true to false and false to true.
a and b are positive numbers.if (a > 0 && b > 0) { // code runs if both a and b are greater than 0 }
x equals 10 or y equals 20.if (x == 10 || y == 20) { // code runs if x is 10 or y is 20 (or both) }
isReady is false, using NOT operator.if (!isReady) { // code runs if isReady is false }
This program uses logical operators to decide if someone can enter based on age and ID, if they can go outside depending on rain or umbrella, and whether to turn on the light if it is off.
#include <iostream> int main() { int age = 25; bool hasID = true; if (age >= 18 && hasID) { std::cout << "Entry allowed" << std::endl; } else { std::cout << "Entry denied" << std::endl; } bool isRaining = false; bool hasUmbrella = true; if (isRaining && !hasUmbrella) { std::cout << "Better stay inside" << std::endl; } else { std::cout << "You can go outside" << std::endl; } bool lightOn = false; if (!lightOn) { std::cout << "Turn on the light" << std::endl; } return 0; }
Remember that && stops checking if the first condition is false (short-circuit).
Similarly, || stops checking if the first condition is true.
Use parentheses to group conditions clearly and avoid confusion.
Logical operators combine true/false checks to control program flow.
&& means both conditions must be true.
|| means at least one condition is true.
! flips true to false and false to true.
Practice
Solution
Step 1: Understand the meaning of logical AND
The operator that requires both conditions to be true is called logical AND.Step 2: Identify the C++ symbol for logical AND
In C++, logical AND is represented by &&.Final Answer:
&& -> Option AQuick Check:
Both true = true means && [OK]
- Confusing & (bitwise AND) with && (logical AND)
- Thinking || means both true
- Mixing ! with AND or OR
flag in C++?Solution
Step 1: Recall the negation operator syntax
The negation operator ! is placed before the variable to flip its value.Step 2: Check correct assignment syntax
To update flag to its negation, write flag = !flag; which is valid C++ syntax.Final Answer:
flag = !flag; -> Option DQuick Check:
Negate with ! before variable [OK]
- Placing ! after variable (invalid syntax)
- Using != which means not equal
- Trying to assign to !flag (not a variable)
bool a = true;
bool b = false;
if (a || b) {
std::cout << "Yes";
} else {
std::cout << "No";
}
Solution
Step 1: Evaluate the logical OR condition
a is true, b is false, so a || b is true because at least one is true.Step 2: Determine which branch runs
Since the condition is true, the if block runs and prints "Yes".Final Answer:
Yes -> Option AQuick Check:
true || false = true [OK]
- Thinking both must be true for ||
- Confusing output with boolean literals
- Ignoring that std::cout prints strings
bool x = true;
bool y = false;
if (x & y) {
std::cout << "True";
} else {
std::cout << "False";
}
Solution
Step 1: Identify the operator used in condition
The code uses & which is bitwise AND, not logical AND.Step 2: Explain why && is needed
For boolean logic in conditions, && should be used to combine true/false values.Final Answer:
Using & instead of && in if condition -> Option CQuick Check:
Logical AND needs && not & [OK]
- Confusing bitwise & with logical &&
- Assuming & works like && in conditions
- Ignoring operator precedence differences
int a = 5; and int b = 10;, which condition correctly checks if a is less than 10 AND b is greater than 5?Solution
Step 1: Understand the condition requirements
We want both: a less than 10 AND b greater than 5.Step 2: Match condition with logical AND
Logical AND (&&) combines both conditions to be true simultaneously.Final Answer:
if (a < 10 && b > 5) -> Option BQuick Check:
Both true means && [OK]
- Using || which means either condition true
- Negating conditions incorrectly
- Swapping comparison operators
Ready to prove it?
Pass the test and this pattern moves unknown
