Ternary conditional operator in Swift - Time & Space Complexity
We want to see how the time needed to run code with a ternary conditional operator changes as input size grows.
Does using this operator affect how long the program takes when inputs get bigger?
Analyze the time complexity of the following code snippet.
let number = 10
let result = number % 2 == 0 ? "Even" : "Odd"
print(result)
This code checks if a number is even or odd using the ternary conditional operator and prints the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single conditional check using the ternary operator.
- How many times: Exactly once, no loops or repeated steps.
Since the code runs just one check, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the code stays constant, no matter how big the input is.
[X] Wrong: "Using the ternary operator makes the code slower as input grows because it's a conditional."
[OK] Correct: The ternary operator only does one check and does not repeat, so input size does not affect its speed.
Understanding that simple conditional checks like the ternary operator run in constant time helps you explain code efficiency clearly and confidently.
"What if we used the ternary operator inside a loop that runs n times? How would the time complexity change?"
Practice
condition ? valueIfTrue : valueIfFalseSolution
Step 1: Understand the operator structure
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.Step 2: Identify its purpose
It selects the first value if the condition is true, otherwise the second value.Final Answer:
Chooses one of two values based on a condition -> Option DQuick Check:
Ternary operator = chooses between two values [OK]
- Confusing ternary with loops
- Thinking it declares variables
- Mixing it with function syntax
Solution
Step 1: Recall the ternary syntax
The correct syntax places the condition first, then a question mark, then the true value, a colon, and the false value.Step 2: Compare options
Only condition ? valueIfTrue : valueIfFalse matches this exact pattern.Final Answer:
condition ? valueIfTrue : valueIfFalse -> Option AQuick Check:
Syntax is condition ? true : false [OK]
- Swapping question mark and colon
- Using commas instead of colon
- Adding semicolons inside ternary
let age = 20 let canVote = age >= 18 ? "Yes" : "No" print(canVote)
Solution
Step 1: Evaluate the condition
age is 20, which is greater than or equal to 18, so the condition is true.Step 2: Apply ternary operator
Since the condition is true, canVote is assigned "Yes".Final Answer:
Yes -> Option BQuick Check:
age >= 18 is true, so output is "Yes" [OK]
- Confusing true and false branches
- Assuming boolean output instead of string
- Ignoring the condition value
let score = 85 let grade = score > 90 ? "A" : "B" else "C"
Solution
Step 1: Analyze ternary operator usage
The ternary operator already includes both true and false values separated by a colon; adding 'else' is invalid syntax.Step 2: Identify the syntax error
The 'else' keyword after the ternary expression causes a syntax error.Final Answer:
Extra 'else' keyword after ternary operator -> Option CQuick Check:
No 'else' after ternary operator [OK]
- Adding 'else' after ternary
- Confusing ternary with if-else blocks
- Misplacing colons and question marks
What is the correct way to write this using dictionary comprehension?
let scores = ["Anna": 75, "Ben": 45, "Cara": 50]
let results = scores.mapValues { score in
// Fill in here
}Solution
Step 1: Understand the condition for passing
Students pass if their score is 50 or more, so condition is score >= 50.Step 2: Apply ternary operator correctly
Use ternary to return "Pass" if true, else "Fail".Final Answer:
score >= 50 ? "Pass" : "Fail" -> Option AQuick Check:
Use condition ? "Pass" : "Fail" for mapping [OK]
- Using if-else instead of ternary in mapValues
- Wrong comparison operator (>)
- Swapping Pass and Fail values
Ready to prove it?
Pass the test and this pattern moves unknown
