For loop as while in Go - Time & Space Complexity
We want to see how the time it takes to run a for loop used like a while loop changes as the input grows.
How does the number of steps grow when the loop runs more times?
Analyze the time complexity of the following code snippet.
package main
func countDown(n int) {
for n > 0 {
n--
}
}
This code counts down from n to zero using a for loop that acts like a while loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs and decreases n by 1 each time.
- How many times: The loop runs exactly n times until n reaches zero.
Each time n gets bigger, the loop runs more times, directly matching n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of steps grows in a straight line with n.
Time Complexity: O(n)
This means the time to finish grows directly with the size of n.
[X] Wrong: "The loop runs a fixed number of times no matter what n is."
[OK] Correct: The loop depends on n and runs once for each count down, so bigger n means more steps.
Understanding how loops grow with input helps you explain your code clearly and shows you know how programs scale.
"What if we changed the loop to decrease n by 2 each time? How would the time complexity change?"
Practice
for loop?Solution
Step 1: Understand Go's while loop syntax
Go usesfor condition { }to act like a while loop.Step 2: Identify the correct syntax
for i < 5 { i++ }usesfor i < 5 { i++ }, which is valid Go syntax for a while loop.Final Answer:
for i < 5 { i++ } -> Option AQuick Check:
Go while loop = for condition { } [OK]
- Using 'while' keyword which Go does not support
- Adding parentheses around the condition unnecessarily
- Using 'loop' keyword which is invalid in Go
Solution
Step 1: Check syntax for Go while loop
Go usesfor condition { }without parentheses around condition.Step 2: Verify loop condition and output
i := 1 for i <= 3 { fmt.Println(i) i++ } usesi <= 3and increments i, printing 1, 2, 3 correctly.Final Answer:
i := 1 for i <= 3 { fmt.Println(i) i++ } -> Option AQuick Check:
Correct Go while loop syntax = i := 1 for i <= 3 { fmt.Println(i) i++ } [OK]
- Using 'while' keyword which is invalid in Go
- Using '<' instead of '<=' causing missing last number
- Adding parentheses around condition causing syntax error
i := 0
for i < 3 {
fmt.Print(i, " ")
i++
}Solution
Step 1: Analyze loop condition and initial value
Variable i starts at 0 and loop runs while i < 3.Step 2: Trace loop iterations and output
i prints 0, then increments to 1; prints 1, increments to 2; prints 2, increments to 3; loop stops.Final Answer:
0 1 2 -> Option BQuick Check:
Loop prints 0 to 2 with spaces [OK]
- Assuming loop includes i=3 output
- Starting i from 1 instead of 0
- Forgetting to increment i causing infinite loop
i := 0
for i < 3 {
fmt.Println(i)
}Solution
Step 1: Check loop condition and body
The loop condition is valid, but i is never changed inside the loop.Step 2: Understand consequences of missing increment
Without incrementing i, the condition i < 3 remains true forever, causing an infinite loop.Final Answer:
Missing increment of i inside the loop causing infinite loop -> Option DQuick Check:
Change loop variable inside loop to avoid infinite loop [OK]
- Forgetting to increment or change loop variable
- Thinking fmt.Println causes error
- Declaring variable inside loop unnecessarily
Solution
Step 1: Understand the loop condition for reading input
We want to keep reading until input equals "exit".Step 2: Analyze options for correct loop behavior
var input string for { fmt.Print("Enter text: ") fmt.Scanln(&input) if input == "exit" { break } } uses an infinite loop with a break when input is "exit", which is safe and common in Go.Step 3: Why other options fail
var input string for input != "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) } uses input before reading it, so loop condition is false initially (empty string != "exit" is true, but input is empty initially). var input string for input == "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) } loops only if input == "exit" which is opposite. var input string while input != "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) } uses invalid 'while' keyword.Final Answer:
Use infinite for loop with break on "exit" input -> Option CQuick Check:
Use for { ... if condition { break } } for while loops with input [OK]
- Using 'while' keyword which is invalid in Go
- Checking condition before first input read
- Using wrong loop condition causing no input read
Ready to prove it?
Pass the test and this pattern moves unknown
