Loop execution flow in Go - Time & Space Complexity
Loops are a common way to repeat actions in code. Understanding how many times a loop runs helps us see how the program's work grows.
We want to know: how does the time to finish change when the loop runs more times?
Analyze the time complexity of the following code snippet.
for i := 0; i < n; i++ {
fmt.Println(i)
}
This code prints numbers from 0 up to n-1, repeating the print action n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The print statement inside the loop.
- How many times: Exactly n times, once for each loop cycle.
As n grows, the number of print actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of loop cycles.
[X] Wrong: "The loop runs instantly no matter how big n is."
[OK] Correct: Each loop cycle takes time, so more cycles mean more total time.
Knowing how loops affect time helps you explain your code clearly and shows you understand how programs grow with input size.
"What if we added a nested loop inside this loop? How would the time complexity change?"
Practice
for loop in Go?Solution
Step 1: Understand the role of loops
Loops are used to repeat actions multiple times based on a condition.Step 2: Identify the purpose of
In Go, theforin Goforloop is the only loop and repeats code while a condition holds true.Final Answer:
To repeat a block of code while a condition is true -> Option CQuick Check:
Loop = repeat code [OK]
- Confusing loops with variable declarations
- Thinking loops handle errors
- Mixing loops with function declarations
for loop in Go that runs 3 times?Solution
Step 1: Check Go loop syntax
Go requires initialization with :=, condition, and post statement inside parentheses without extra parentheses around the whole for.Step 2: Validate each option
for i := 0; i < 3; i++ { } uses correct syntax:for i := 0; i < 3; i++ { }. Options B and C have wrong parentheses or missing :=. for i := 0; i < 3; i++ misses braces.Final Answer:
for i := 0; i < 3; i++ { } -> Option DQuick Check:
Correct Go for loop syntax = for i := 0; i < 3; i++ { } [OK]
- Using parentheses like C/Java
- Missing := for variable declaration
- Omitting braces {}
for i := 1; i <= 3; i++ {
if i == 2 {
continue
}
fmt.Print(i)
}Solution
Step 1: Understand the loop and continue
The loop runs i from 1 to 3. When i equals 2,continueskips printing.Step 2: Trace output for each i
i=1 prints '1', i=2 skips print, i=3 prints '3'. Output is '13'.Final Answer:
13 -> Option AQuick Check:
Continue skips i=2 print = 13 [OK]
- Assuming continue prints the skipped value
- Confusing continue with break
- Ignoring loop condition
for i := 0; i < 5; i++
fmt.Println(i)
Solution
Step 1: Check Go loop syntax rules
Go requires braces {} to define the loop body even if it is one statement.Step 2: Identify missing braces
The code lacks braces aroundfmt.Println(i), causing a syntax error.Final Answer:
Missing braces {} around loop body -> Option AQuick Check:
Braces required for loop body [OK]
- Omitting braces for single statement
- Misreading error as variable issue
- Confusing println with Println
sum := 0
for i := 1; i <= 5; i++ {
if i%2 == 0 {
continue
}
if sum > 5 {
break
}
sum += i
}
fmt.Println(sum)Solution
Step 1: Trace loop with continue and break
Loop i=1 to 5, skip even i (2,4). Add odd i to sum until sum > 5, then break.Step 2: Calculate sum step-by-step
i=1 (odd): sum=0+1=1
i=2 (even): continue, skip
i=3 (odd): sum=1+3=4
i=4 (even): continue, skip
i=5 (odd): sum=4+5=9 but before adding, sum=4 which is not >5, so add 5 sum=9. Now sum >5, break after this iteration.Step 3: Check when break occurs
Break happens after sum=9, but condition checks sum >5 before adding i=5, so sum was 4 before adding 5. Actually, break triggers only if sum >5 before adding i.Step 4: Correct calculation
At i=5, sum=4 (not >5), so add 5 sum=9. Next iteration would be i=6 (loop ends). So break never triggers because sum >5 check is before adding i.Final Answer:
9 -> Option BQuick Check:
Sum after adding odd numbers until break = 9 [OK]
- Adding after break condition incorrectly
- Ignoring continue skipping even numbers
- Misunderstanding break timing
Ready to prove it?
Pass the test and this pattern moves unknown
