While loop in Swift - Time & Space Complexity
We want to understand how the time a while loop takes changes as the input grows.
Specifically, how many times does the loop run when the input gets bigger?
Analyze the time complexity of the following code snippet.
var count = 0
let n = 100
while count < n {
print(count)
count += 1
}
This code prints numbers from 0 up to n-1 using a while loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs repeatedly.
- How many times: It runs once for each number from 0 to n-1, so n times.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times |
| 100 | About 100 times |
| 1000 | About 1000 times |
Pattern observation: The operations increase directly with n; if n doubles, operations double.
Time Complexity: O(n)
This means the time grows in a straight line with the input size.
[X] Wrong: "The while loop runs a fixed number of times no matter the input."
[OK] Correct: The loop runs as many times as n, so bigger n means more runs.
Understanding how loops grow with input size helps you explain your code clearly and shows you know how to write efficient programs.
"What if we changed the loop to count down from n to 0? How would the time complexity change?"
Practice
while loop do in Swift?Solution
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly as long as its condition stays true.Step 2: Compare options with the definition
Only Repeats code while a condition is true correctly describes repeating code while a condition is true.Final Answer:
Repeats code while a condition is true -> Option AQuick Check:
While loop = repeat while true condition [OK]
- Thinking while loops run a fixed number of times
- Confusing while with if statements
- Assuming while loops run only once
Solution
Step 1: Recall Swift while loop syntax
Swift uses: while condition { code } without parentheses or extra keywords.Step 2: Check each option
while condition { /* code */ } matches Swift syntax exactly; others have extra keywords or wrong punctuation.Final Answer:
while condition { /* code */ } -> Option DQuick Check:
Swift while syntax = while condition { } [OK]
- Adding parentheses around the condition
- Using 'do' keyword incorrectly
- Using colon instead of braces
var count = 3
while count > 0 {
print(count)
count -= 1
}Solution
Step 1: Analyze the loop condition and variable
Variable count starts at 3 and loop runs while count > 0.Step 2: Trace each iteration
Prints 3, then count becomes 2; prints 2, count 1; prints 1, count 0; loop stops.Final Answer:
3 2 1 -> Option BQuick Check:
Loop prints count down from 3 to 1 [OK]
- Including 0 in output
- Printing in ascending order
- Not decreasing count inside loop
var i = 1
while i < 5 {
print(i)
}Solution
Step 1: Check loop structure and variable changes
The loop condition depends on i, but i never changes inside the loop.Step 2: Understand effect of missing increment
Without increasing i, the condition i < 5 stays true forever, causing an infinite loop.Final Answer:
Missing increment of i inside the loop -> Option CQuick Check:
Loop variable must change to avoid infinite loop [OK]
- Forgetting to increment or change loop variable
- Thinking print causes error
- Assuming syntax is wrong
Solution
Step 1: Plan loop to add even numbers from 2 to 10
Start with i = 2, add i to sum, then increase i by 2 to get next even number.Step 2: Check options for correct approach
Use a variable starting at 2, add it to sum, increment by 2 until 10 correctly describes starting at 2, adding to sum, incrementing by 2 until 10.Final Answer:
Use a variable starting at 2, add it to sum, increment by 2 until 10 -> Option AQuick Check:
Sum evens by stepping 2 from 2 to 10 [OK]
- Incrementing by 2 without starting at 2
- Starting from 10 and incrementing
- Multiplying sum incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
