For loop basics in Go - Time & Space Complexity
When we use a for loop, the program repeats some steps many times. Understanding how this repetition grows helps us know how fast or slow the program runs.
We want to find out how the number of repeated steps changes as 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 step n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The print statement inside the for loop.
- How many times: Exactly n times, once for each loop cycle.
As n grows, the number of print steps grows the same way. If n doubles, the steps double too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print steps |
| 100 | 100 print steps |
| 1000 | 1000 print steps |
Pattern observation: The work grows directly with n, so more input means more steps in a straight line.
Time Complexity: O(n)
This means the time it takes grows in direct proportion to the number of times the loop runs.
[X] Wrong: "The loop runs instantly no matter how big n is."
[OK] Correct: Each loop step takes some time, so more steps mean more total time. The program does not skip the repeated work.
Knowing how loops affect time helps you explain your code clearly and shows you understand how programs grow with input size. This skill is useful in many coding situations.
"What if we added a nested loop inside this loop? How would the time complexity change?"
Practice
for loop do in Go programming?Solution
Step 1: Understand the purpose of a for loop
A for loop is used to repeat code multiple times based on a condition.Step 2: Identify the correct description
Only Repeats a block of code while a condition is true correctly describes repeating code while a condition is true.Final Answer:
Repeats a block of code while a condition is true -> Option DQuick Check:
For loop = Repeat code [OK]
- Thinking for loop stops program
- Confusing for loop with variable declaration
- Mixing for loop with import statements
Solution
Step 1: Recall Go for loop syntax
Go usesfor init; condition; post { }syntax without parentheses.Step 2: Compare options
for i := 0; i < 5; i++ { } matches Go syntax exactly. Options B, C, and D use other languages or invalid syntax.Final Answer:
for i := 0; i < 5; i++ { } -> Option AQuick Check:
Go for loop = for init; condition; post [OK]
- Using parentheses like in C or Java
- Using Python-style for-in loops
- Writing 'loop' instead of 'for'
for i := 1; i <= 3; i++ {
fmt.Print(i, " ")
}Solution
Step 1: Analyze loop range and output
The loop starts at i=1 and runs while i <= 3, so i takes values 1, 2, 3.Step 2: Understand print output
Each iteration prints i followed by a space, so output is "1 2 3 "Final Answer:
1 2 3 -> Option AQuick Check:
Loop prints 1 to 3 with spaces [OK]
- Starting loop at 0 instead of 1
- Including extra number beyond 3
- Confusing print with println
for i := 0; i < 5 i++ {
fmt.Println(i)
}Solution
Step 1: Check for loop syntax parts
Go requires semicolons between init, condition, and post parts in for loops.Step 2: Locate missing semicolon
The code misses a semicolon betweeni < 5andi++.Final Answer:
Missing semicolon between condition and post statement -> Option BQuick Check:
For loop parts separated by semicolons [OK]
- Omitting semicolons in for loop header
- Confusing := with = in loop variable
- Forgetting curly braces
Solution
Step 1: Understand the goal
We want to add even numbers from 2 to 10 inclusive.Step 2: Analyze each option
for i := 2; i < 10; i += 2 { sum += i } increments by 2 starting at 2, but i < 10 so sums only 2+4+6+8 (misses 10).
for i := 1; i <= 10; i++ { if i%2 == 0 { sum += i } } loops from 1 to 10, checks if number is even, then adds it. This is correct and clear.
for i := 2; i < 10; i++ { sum += i*2 } incorrectly multiplies i by 2 and sums, which is wrong.
for i := 0; i <= 10; i += 2 { sum += i } starts at 0, which includes 0 in sum, not desired.Final Answer:
for i := 1; i <= 10; i++ { if i%2 == 0 { sum += i } } -> Option CQuick Check:
Check even with modulo inside loop [OK]
- Starting loop at 0 including unwanted numbers
- Multiplying instead of checking evenness
- Forgetting to check condition inside loop
Ready to prove it?
Pass the test and this pattern moves unknown
