Why loops are needed in Go - Performance Analysis
Loops help us repeat actions many times without writing the same code again and again.
We want to see how the time to run code changes when we use loops with bigger inputs.
Analyze the time complexity of the following code snippet.
for i := 0; i < n; i++ {
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, doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as the input size grows.
[X] Wrong: "The loop runs only once no matter how big n is."
[OK] Correct: The loop runs once for each number from 0 to n-1, so bigger n means more repeats.
Understanding how loops affect time helps you explain how your code handles bigger tasks clearly and confidently.
"What if we added a nested loop inside this loop? How would the time complexity change?"
Practice
for in Go programming?Solution
Step 1: Understand the purpose of loops
Loops allow repeating actions multiple times without writing the same code again.Step 2: Identify the correct use case
Using loops saves time and makes code shorter and easier to manage.Final Answer:
To repeat a set of instructions multiple times without rewriting code -> Option DQuick Check:
Loops repeat actions = A [OK]
- Thinking loops store data
- Confusing loops with variable creation
- Believing loops stop program execution
for loop in Go?Solution
Step 1: Recall Go loop syntax
Go usesforwith initialization, condition, and post statement separated by semicolons.Step 2: Match syntax to options
for i := 0; i < 5; i++ { fmt.Println(i) } matches Go'sforloop syntax exactly.Final Answer:
for i := 0; i < 5; i++ { fmt.Println(i) } -> Option AQuick Check:
Go uses for with semicolons = C [OK]
- Using while instead of for
- Using loop keyword which Go doesn't have
- Missing semicolons in for loop
for i := 1; i <= 3; i++ {
fmt.Print(i, " ")
}Solution
Step 1: Analyze loop range
The loop starts at i=1 and runs while i is less than or equal to 3, incrementing by 1 each time.Step 2: Trace output for each iteration
It prints 1, then 2, then 3, each followed by a space.Final Answer:
1 2 3 -> Option AQuick Check:
Loop prints 1 to 3 = D [OK]
- Starting loop from 0 instead of 1
- Including extra numbers beyond 3
- Printing in reverse order
for i := 0; i < 5 i++ {
fmt.Println(i)
}Solution
Step 1: Check for syntax errors in for loop
Go requires semicolons separating initialization, condition, and post statements in for loops.Step 2: Locate missing semicolon
The code misses a semicolon betweeni < 5andi++.Final Answer:
Missing semicolon between condition and increment -> Option CQuick Check:
For loop parts need semicolons = A [OK]
- Omitting semicolons in for loop header
- Using while instead of for
- Forgetting braces for loop body
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive.Step 2: Check each option
for i := 2; i < 10; i += 2 { fmt.Println(i) } prints 2,4,6,8 missing 10.
for i := 1; i <= 10; i++ { if i%2 == 0 { fmt.Println(i) } } loops 1 to 10 and prints only if number is even.
for i := 2; i < 10; i++ { if i%2 == 0 { fmt.Println(i) } } loops 2 to 9, missing 10.
for i := 2; i <= 10; i++ { fmt.Println(i*2) } prints multiples of i*2, which gives 4,6,8,... not correct.Step 3: Identify the correct option
for i := 1; i <= 10; i++ { if i%2 == 0 { fmt.Println(i) } } correctly prints all even numbers from 2 to 10 inclusive.Final Answer:
for i := 1; i <= 10; i++ { if i%2 == 0 { fmt.Println(i) } } -> Option BQuick Check:
Check condition inside loop for even numbers = B [OK]
- Forgetting to include 10 in range
- Incrementing by wrong step
- Printing wrong multiples
Ready to prove it?
Pass the test and this pattern moves unknown
