While and do-while loops in Kotlin - Time & Space Complexity
We want to understand how the time it takes to run while and do-while loops changes as the input grows.
Specifically, how many times the loop runs affects the total work done.
Analyze the time complexity of the following code snippet.
var count = 0
while (count < n) {
println("Count is: $count")
count++
}
var index = 0
do {
println("Index is: $index")
index++
} while (index < n)
This code runs two loops: a while loop and a do-while loop, each running until a counter reaches n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The body of each loop runs once per count from 0 up to n - 1.
- How many times: Each loop runs approximately n times.
As n grows, the number of times the loops run grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times each loop runs |
| 100 | About 100 times each loop runs |
| 1000 | About 1000 times each loop runs |
Pattern observation: The work grows directly in proportion to n.
Time Complexity: O(n)
This means the time to run these loops grows linearly as the input size n grows.
[X] Wrong: "A do-while loop always runs faster than a while loop because it runs at least once."
[OK] Correct: Both loops run about n times here, so their time depends on n, not on the loop type.
Understanding how loops grow with input size helps you explain your code's efficiency clearly and confidently.
"What if we added a nested loop inside the while loop that also runs n times? How would the time complexity change?"
Practice
while loop and a do-while loop in Kotlin?Solution
Step 1: Understand
Awhileloop behaviorwhileloop checks its condition before running the code inside it, so if the condition is false initially, the code inside won't run at all.Step 2: Understand
Ado-whileloop behaviordo-whileloop runs the code inside once first, then checks the condition to decide if it should repeat.Final Answer:
whilechecks the condition before running the code;do-whileruns code once before checking. -> Option CQuick Check:
Loop condition check timing =whilechecks the condition before running the code;do-whileruns code once before checking. [OK]
- Thinking both loops check condition before running code
- Confusing which loop runs code first
- Assuming do-while runs only once
do-while loop in Kotlin?Solution
Step 1: Recall correct
Thedo-whilesyntaxdo-whileloop syntax starts withdo, then the code block in braces, followed bywhilewith the condition in parentheses.Step 2: Compare options to syntax
do { /* code */ } while (condition) matches the correct syntax exactly:do { /* code */ } while (condition). Others have wrong order or misplaced keywords.Final Answer:
do { /* code */ } while (condition) -> Option AQuick Check:
Syntax order for do-while = do { /* code */ } while (condition) [OK]
- Placing while before do
- Using parentheses incorrectly
- Mixing up braces and parentheses
var count = 3
while (count > 0) {
println(count)
count--
}Solution
Step 1: Analyze initial value and condition
The variablecountstarts at 3. The loop runs whilecount > 0, so it runs for 3, 2, and 1.Step 2: Trace each iteration and output
Each loop prints the currentcountthen decreases it by 1. So it prints 3, then 2, then 1, then stops before printing 0.Final Answer:
3 2 1 -> Option AQuick Check:
Loop prints count down from 3 to 1 = 3 2 1 [OK]
- Including 0 in output
- Printing in ascending order
- Not decrementing count
var i = 1
do {
println(i)
i++
} while i <= 3Solution
Step 1: Check
Thedo-whilesyntaxwhilecondition must be inside parentheses after thewhilekeyword.Step 2: Identify the error in the code
The code haswhile i <= 3without parentheses. It should bewhile (i <= 3).Final Answer:
Missing parentheses around the while condition -> Option DQuick Check:
Parentheses required for while condition = Missing parentheses around the while condition [OK]
- Omitting parentheses in while condition
- Placing variable declaration inside loop unnecessarily
- Confusing braces and parentheses
Solution
Step 1: Understand the problem requirement
The program must ask the user at least once to enter a number, then repeat if the number is not positive.Step 2: Choose the correct loop type
Ado-whileloop runs the code block once before checking the condition, so it ensures the user is asked at least once.Final Answer:
Use ado-whileloop because it asks the user at least once before checking the condition. -> Option BQuick Check:
Ask user once then check = do-while loop = Use ado-whileloop because it asks the user at least once before checking the condition. [OK]
- Choosing while loop which may skip asking once
- Thinking do-while never checks condition
- Confusing loop behavior with input requirements
Ready to prove it?
Pass the test and this pattern moves unknown
