If and if-else statements in Swift - Time & Space Complexity
We want to see how the time it takes to run code with if and if-else statements changes as the input changes.
Specifically, does a larger input value make the program take longer when using these statements?
Analyze the time complexity of the following code snippet.
func checkNumber(_ num: Int) {
if num > 0 {
print("Positive")
} else if num < 0 {
print("Negative")
} else {
print("Zero")
}
}
This code checks if a number is positive, negative, or zero and prints a message accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else check on one number.
- How many times: The checks happen once per function call.
Each time you call the function with a number, it does a few quick checks only once.
| Example Input | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations remains constant (1 check) regardless of the input value.
Time Complexity: O(1)
This means each if or if-else check takes the same small amount of time no matter what the input number is.
[X] Wrong: "If statements take longer when the number is bigger because it has to check more conditions."
[OK] Correct: The if-else checks only a fixed number of conditions, so the size or value of the number does not make it slower.
Understanding that simple if and if-else statements run quickly and do not slow down with bigger inputs helps you explain how your code handles decisions efficiently.
"What if we added a loop inside the if statement that runs n times? How would the time complexity change?"
Practice
if statement do in Swift?Solution
Step 1: Understand the purpose of
Anifstatementifstatement checks a condition and runs code only when that condition is true.Step 2: Compare with other options
Repeating code is done by loops, storing data is done by variables, and defining functions usesfunc.Final Answer:
It runs code only if a condition is true. -> Option AQuick Check:
If statement = runs code if true [OK]
- Confusing if with loops
- Thinking if stores data
- Mixing if with function definitions
Solution
Step 1: Recall Swift if-else syntax
Swift usesif condition { }andelse { }blocks without extra keywords like 'then' or colons.Step 2: Check each option
if condition { } else { } matches Swift syntax exactly. Options A, B, and C use invalid keywords or punctuation.Final Answer:
if condition { } else { } -> Option AQuick Check:
Swift if-else = if condition { } else { } [OK]
- Adding 'then' keyword
- Using colons after if or else
- Putting condition in parentheses unnecessarily
let number = 10
if number > 5 {
print("Big")
} else {
print("Small")
}Solution
Step 1: Evaluate the condition
The variablenumberis 10. The conditionnumber > 5is true because 10 is greater than 5.Step 2: Determine which block runs
Since the condition is true, the code inside theifblock runs, printing "Big".Final Answer:
Big -> Option DQuick Check:
10 > 5 is true, so prints "Big" [OK]
- Confusing which block runs
- Printing variable instead of string
- Ignoring condition result
let age = 18
if age >= 18
print("Adult")
else
print("Minor")Solution
Step 1: Check Swift if-else syntax
Swift requires braces { } around the code blocks after if and else.Step 2: Identify missing braces
The code lacks braces afterif age >= 18andelse, causing a syntax error.Final Answer:
Missing braces { } around if and else blocks -> Option CQuick Check:
Swift needs braces for if-else blocks [OK]
- Omitting braces in if-else
- Using wrong operators
- Wrong variable types
num. Which code correctly uses if-else statements to do this?Solution
Step 1: Understand the conditions needed
We need to check ifnumis greater than 0, less than 0, or exactly 0.Step 2: Analyze each option
if num > 0 { print("Positive") } else if num < 0 { print("Negative") } else { print("Zero") } correctly checks >0, then <0, else zero. if num >= 0 { print("Positive") } else if num <= 0 { print("Negative") } else { print("Zero") } wrongly overlaps conditions. if num > 0 { print("Positive") } else { print("Negative") } print("Zero") prints "Zero" always. if num == 0 { print("Zero") } else if num > 0 { print("Negative") } else if num < 0 { print("Positive") } incorrectly prints "Negative" for positive numbers and "Positive" for negative numbers.Final Answer:
if num > 0 { print("Positive") } else if num < 0 { print("Negative") } else { print("Zero") } -> Option BQuick Check:
Check >0, else if <0, else zero [OK]
- Overlapping conditions causing wrong output
- Printing zero outside else block
- Wrong order of conditions
Ready to prove it?
Pass the test and this pattern moves unknown
