Logical operators in C++ - Time & Space Complexity
Logical operators combine true or false values to make decisions in code.
We want to see how the time it takes to run code with logical operators changes as input grows.
Analyze the time complexity of the following code snippet.
bool checkAllTrue(const std::vector<bool>& flags) {
for (bool flag : flags) {
if (!flag) {
return false;
}
}
return true;
}
This code checks if all values in a list are true using logical NOT and a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the list.
- How many times: Up to once per element, stopping early if a false is found.
As the list gets bigger, the code checks more items, but may stop early if a false appears.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows roughly in direct proportion to the list size.
Time Complexity: O(n)
This means the time to run grows linearly with the number of items to check.
[X] Wrong: "Logical operators make the code run instantly regardless of input size."
[OK] Correct: Logical operators themselves are simple, but when used inside loops, the total time depends on how many times the loop runs.
Understanding how logical checks inside loops affect time helps you explain code efficiency clearly and confidently.
"What if we changed the loop to check pairs of elements at once? How would the time complexity change?"
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
