Logical operators in Swift - Time & Space Complexity
Logical operators combine true or false values to make decisions in code.
We want to see how the time to run code with logical operators changes as input grows.
Analyze the time complexity of the following code snippet.
func checkConditions(_ values: [Bool]) -> Bool {
for value in values {
if value && someOtherCheck() {
return true
}
}
return false
}
func someOtherCheck() -> Bool {
return true
}
This code checks each Boolean in an array and uses logical AND to decide if it should return true early.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of Booleans.
- How many times: Up to once per element, until a true condition is found.
As the array gets bigger, the loop may run more times, but it can stop early if condition is met.
| 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 input size.
Time Complexity: O(n)
This means the time to run grows linearly with the number of items to check.
[X] Wrong: "Logical operators always make code run faster, so time complexity is constant."
[OK] Correct: Logical operators themselves are quick, but if used inside loops over many items, total time still grows with input size.
Understanding how logical operators affect loops helps you explain code efficiency clearly and confidently.
"What if the function someOtherCheck() took longer time to run? How would the time complexity change?"
Practice
true only if both conditions are true?Solution
Step 1: Understand the AND operator
The AND operator (&&) returns true only when both conditions it connects are true.Step 2: Compare with other operators
The OR operator (||) returns true if at least one condition is true, NOT (!) flips true to false and vice versa, and == checks equality, not logical combination.Final Answer:
&& (AND) -> Option CQuick Check:
AND = && [OK]
- Confusing AND (&&) with OR (||)
- Thinking NOT (!) combines conditions
- Mixing equality (==) with logical operators
isActive in Swift?Solution
Step 1: Recall the NOT operator syntax
In Swift, the NOT operator is written as an exclamation mark (!) placed before the Boolean variable to flip its value.Step 2: Check each option
!isActive is the correct syntax. The other options are not valid Swift syntax for negation.Final Answer:
!isActive -> Option AQuick Check:
NOT = ! before variable [OK]
- Writing 'not' instead of '!'
- Putting ! after variable name
- Using method calls like .not()
let a = true let b = false print(a || b && !b)
Solution
Step 1: Evaluate the NOT operator
!b means !false which is true.Step 2: Evaluate AND operator
b && !b is false && true which is false.Step 3: Evaluate OR operator
a || (result of AND) is true || false which is true.Final Answer:
true -> Option BQuick Check:
OR with true = true [OK]
- Ignoring operator precedence
- Evaluating OR before AND
- Misunderstanding ! operator
let x = true
let y = false
if x || && y {
print("Yes")
}Solution
Step 1: Analyze the if condition syntax
The condition uses 'x || && y' which combines OR (||) and AND (&&) operators without a proper operand between them.Step 2: Identify the logical operator misuse
Logical operators must connect two Boolean expressions. Here, '|| &&' is invalid syntax.Final Answer:
Incorrect use of logical operators together -> Option DQuick Check:
Operators must connect expressions properly [OK]
- Writing two operators without a condition between
- Assuming parentheses fix operator misuse
- Confusing variable types with operator errors
n is between 10 and 20 inclusive in Swift. Which condition correctly uses logical operators?Solution
Step 1: Understand the range condition
To check if n is between 10 and 20 inclusive, n must be greater than or equal to 10 AND less than or equal to 20.Step 2: Evaluate each option
n >= 10 || n <= 20 uses OR which is incorrect because it allows values outside the range. n > 10 && n < 20 excludes 10 and 20 by using > and <. !(n < 10 && n > 20) negates an impossible condition and is incorrect.Final Answer:
n >= 10 && n <= 20 -> Option AQuick Check:
Use AND for range checks [OK]
- Using OR instead of AND for range
- Excluding boundary values by using > or <
- Using negation incorrectly for range
Ready to prove it?
Pass the test and this pattern moves unknown
