Var for variables (mutable) in Swift - Time & Space Complexity
Let's see how using var for variables affects the time it takes for a program to run.
We want to know how the program's steps grow when we change or update variables.
Analyze the time complexity of the following code snippet.
var total = 0
let numbers = [1, 2, 3, 4, 5]
for number in numbers {
total += number
}
print(total)
This code adds up all numbers in an array using a mutable variable total.
Look for loops or repeated steps.
- Primary operation: Adding each number to
totalinside the loop. - How many times: Once for each number in the array.
As the list of numbers gets bigger, the program adds more numbers one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of steps grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input size grows.
[X] Wrong: "Using var makes the code slower because variables change all the time."
[OK] Correct: Changing a variable itself is quick; the main time cost comes from how many times you do it, not that it changes.
Understanding how loops and variable changes affect time helps you explain your code clearly and think about efficiency in real projects.
"What if we replaced the for loop with a function that sums the array recursively? How would the time complexity change?"
Practice
var keyword do in Swift?Solution
Step 1: Understand the purpose of
Thevarvarkeyword is used to declare variables that can be changed after they are set.Step 2: Compare with constants
Unlikelet, which creates constants,varallows the value to be updated.Final Answer:
It creates a variable whose value can change later. -> Option AQuick Check:
varmeans mutable variable [OK]
- Confusing var with let (constant)
- Thinking var creates constants
- Mixing var with function declaration
score with initial value 10 in Swift?Solution
Step 1: Identify the keyword for mutable variables
In Swift,vardeclares a variable that can change, so usevar score = 10.Step 2: Check other options
letcreates a constant,variableandconstare not valid Swift keywords.Final Answer:
var score = 10 -> Option DQuick Check:
Mutable variable usesvar[OK]
- Using let instead of var for mutable variables
- Using invalid keywords like variable or const
- Missing the equals sign
var count = 5 count = 10 print(count)
Solution
Step 1: Understand variable assignment
The variablecountis declared withvarand initially set to 5, so it can be changed.Step 2: Update and print value
The value is changed to 10 before printing, soprint(count)outputs 10.Final Answer:
10 -> Option AQuick Check:
Mutable var updates value correctly [OK]
- Thinking var acts like let (constant)
- Expecting original value instead of updated
- Confusing syntax causing errors
var name = "Alice" name = 42 print(name)
Solution
Step 1: Check variable type inference
Variablenameis inferred as String because of initial value "Alice".Step 2: Assign incompatible type
Assigning an integer 42 to a String variable causes a type mismatch error.Final Answer:
Type mismatch error when assigning 42 to name -> Option CQuick Check:
Swift variables keep type, cannot assign different type [OK]
- Ignoring type mismatch errors
- Thinking var allows any type changes
- Confusing syntax error with type error
var?
1. let score = 0 2. score = 10 3. var score = 0 4. score = 10 5. var score: String 6. score = 10 7. let score: Int 8. score = 10
Solution
Step 1: Identify mutable variable declaration
Lines 3 and 4 declarescorewithvarand assign 0, then update to 10, which is correct.Step 2: Check other options
Lines 1 and 2 uselet(constant), so line 2 causes error. Lines 5 and 6 declarescoreasStringbut assign integer 10, causing type mismatch error. Lines 7 and 8 useletconstant and then try to assign, causing error.Final Answer:
Lines 3 and 4 -> Option BQuick Check:
Use var to declare and update variables [OK]
- Using let for variables that change
- Type mismatch with wrong type annotation
- Confusing var and let usage
Ready to prove it?
Pass the test and this pattern moves unknown
