Bird
Raised Fist0
Goprogramming~10 mins

Nested loops in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Nested loops
Start outer loop i=0
↓
Start inner loop j=0
↓
Execute inner loop body
↓
Update inner loop j=j+1
↓
Check inner loop j < limit?
Yes→Repeat inner loop body
No↓
Update outer loop i=i+1
↓
Check outer loop i < limit?
Yes→Repeat inner loop
No↓
End
The outer loop runs first. For each outer loop step, the inner loop runs completely. Then the outer loop moves to the next step.
Execution Sample
Go
for i := 0; i < 2; i++ {
    for j := 0; j < 3; j++ {
        fmt.Println(i, j)
    }
}
Print pairs of i and j where i goes 0 to 1 and j goes 0 to 2 for each i.
Execution Table
StepijCondition i<2Condition j<3ActionOutput
100truetruePrint (0,0)(0 0)
201truetruePrint (0,1)(0 1)
302truetruePrint (0,2)(0 2)
403truefalseInner loop ends
510truetruePrint (1,0)(1 0)
611truetruePrint (1,1)(1 1)
712truetruePrint (1,2)(1 2)
813truefalseInner loop ends
920falsetrueOuter loop ends
💡 Outer loop i=2 fails condition i<2, so loops stop.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8Final
i0000011112
j0123012300
Key Moments - 3 Insights
Why does j reset to 0 after inner loop ends?
Because j is declared inside the inner loop, it starts fresh each time the inner loop begins (see rows 4 and 5 in execution_table).
Why does the outer loop increment only after inner loop finishes?
The outer loop waits for the inner loop to complete all its steps before increasing i (see transition from row 4 to 5).
What stops the loops from running forever?
The conditions i<2 and j<3 become false eventually, stopping loops (see rows 4 and 9 where conditions fail).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of j at step 6?
A2
B1
C0
D3
💡 Hint
Check the 'j' column in row 6 of the execution_table.
At which step does the outer loop condition become false?
AStep 9
BStep 8
CStep 4
DStep 7
💡 Hint
Look at the 'Condition i<2' column and find where it is false.
If the inner loop condition changed to j<2, how many times would the inner loop print per outer loop iteration?
A3 times
B1 time
C2 times
D4 times
💡 Hint
Check how many times j runs from 0 while j<2 in the variable_tracker.
Concept Snapshot
Nested loops run one loop inside another.
Outer loop runs first.
Inner loop runs fully for each outer loop step.
Inner loop variable resets each time.
Loops stop when conditions fail.
Full Transcript
This example shows nested loops in Go. The outer loop variable i starts at 0 and runs while less than 2. For each i, the inner loop variable j runs from 0 to less than 3. Each pair (i,j) is printed. The inner loop finishes all its steps before the outer loop increments i. Variables i and j change as loops run. The loops stop when conditions become false. This helps understand how loops inside loops work step-by-step.

Practice

(1/5)
1. What does a nested loop in Go do?
for i := 0; i < 3; i++ {
for j := 0; j < 2; j++ {
// code
}
}
easy
A. Runs one loop inside another to repeat code multiple times
B. Runs two loops one after another without overlap
C. Runs only the inner loop once
D. Creates an infinite loop automatically

Solution

  1. Step 1: Understand nested loops structure

    The outer loop runs 3 times, and for each outer loop iteration, the inner loop runs 2 times.
  2. Step 2: Explain the repetition effect

    This means the inner code runs 3 * 2 = 6 times, repeating actions multiple times inside another loop.
  3. Final Answer:

    Runs one loop inside another to repeat code multiple times -> Option A
  4. Quick Check:

    Nested loops repeat code multiple times [OK]
Hint: Nested loops repeat inner code for each outer loop cycle [OK]
Common Mistakes:
  • Thinking loops run sequentially without nesting
  • Assuming inner loop runs only once
  • Believing nested loops cause infinite loops automatically
2. Which of the following is the correct syntax for a nested loop in Go?
easy
A. for i := 0; i < 3; i++; for j := 0; j < 2; j++ { fmt.Println(i, j) }
B. for i := 0; i < 3; i++ { for j < 2 { fmt.Println(i, j); j++ } }
C. for i := 0; i < 3; i++ { for j := 0; j < 2; j++ } fmt.Println(i, j)
D. for i := 0; i < 3; i++ { for j := 0; j < 2; j++ { fmt.Println(i, j) } }

Solution

  1. Step 1: Check correct nested loop syntax

    for i := 0; i < 3; i++ { for j := 0; j < 2; j++ { fmt.Println(i, j) } } correctly nests the inner for loop inside the outer loop with braces and increments.
  2. Step 2: Identify syntax errors in other options

    for i := 0; i < 3; i++ { for j < 2 { fmt.Println(i, j); j++ } } uses a while-style loop without declaring j, causing an undefined variable error in Go. for i := 0; i < 3; i++; for j := 0; j < 2; j++ { fmt.Println(i, j) } has two for loops separated by a semicolon, which is invalid. for i := 0; i < 3; i++ { for j := 0; j < 2; j++ } fmt.Println(i, j) places the print statement outside the inner loop braces, causing scope issues.
  3. Final Answer:

    for i := 0; i < 3; i++ { for j := 0; j < 2; j++ { fmt.Println(i, j) } } -> Option D
  4. Quick Check:

    Correct nested loop syntax = for i := 0; i < 3; i++ { for j := 0; j < 2; j++ { fmt.Println(i, j) } } [OK]
Hint: Remember Go uses braces and semicolons inside for loops [OK]
Common Mistakes:
  • Using semicolon between two for loops instead of nesting
  • Trying while-style loops without proper syntax
  • Placing code outside loop braces incorrectly
3. What is the output of this Go program?
package main
import "fmt"
func main() {
  for i := 1; i <= 2; i++ {
    for j := 1; j <= 3; j++ {
      fmt.Print(i * j, " ")
    }
    fmt.Println()
  }
}
medium
A. 2 4 6 1 2 3
B. 1 1 1 2 2 2
C. 1 2 3 2 4 6
D. 1 2 3 4 5 6

Solution

  1. Step 1: Analyze outer loop iterations

    The outer loop runs for i = 1 and i = 2.
  2. Step 2: Calculate inner loop outputs for each i

    For i=1, inner loop prints 1*1=1, 1*2=2, 1*3=3. For i=2, inner loop prints 2*1=2, 2*2=4, 2*3=6.
  3. Final Answer:

    1 2 3 2 4 6 -> Option C
  4. Quick Check:

    Multiplying i and j in nested loops = 1 2 3 2 4 6 [OK]
Hint: Multiply outer and inner loop counters for output [OK]
Common Mistakes:
  • Confusing loop ranges or order
  • Printing all numbers in one line without newline
  • Mixing up i and j in multiplication
4. Find the error in this nested loop code snippet:
for i := 0; i < 3; i++ {
  for j := 0; j < 2; j++
    fmt.Println(i, j)
}
medium
A. Missing braces {} for inner loop body
B. Outer loop condition is incorrect
C. Inner loop variable j is not declared
D. fmt.Println cannot be used inside loops

Solution

  1. Step 1: Check inner loop syntax

    The inner loop lacks braces {} around its body, which is required for multiple statements or to clearly define the loop block in Go.
  2. Step 2: Understand Go syntax rules

    Without braces, only the next statement is considered inside the loop. Here, fmt.Println is intended as the loop body but braces improve clarity and prevent errors.
  3. Final Answer:

    Missing braces {} for inner loop body -> Option A
  4. Quick Check:

    Braces needed for loop body in Go [OK]
Hint: Always use braces {} for nested loop bodies [OK]
Common Mistakes:
  • Assuming braces are optional for loops
  • Thinking variable declaration is missing
  • Believing fmt.Println can't be inside loops
5. You want to print a multiplication table from 1 to 4 using nested loops in Go. Which code snippet correctly achieves this?
hard
A. for i := 1; i < 4; i++ { for j := 1; j < 4; j++ { fmt.Println(i*j) } }
B. for i := 1; i <= 4; i++ { for j := 1; j <= 4; j++ { fmt.Printf("%d ", i*j) } fmt.Println() }
C. for i := 1; i <= 4; i++ { for j := 1; j <= 4; j++ { fmt.Println(i+j) } fmt.Println() }
D. for i := 1; i <= 4; i++ { for j := 1; j <= 4; j++ { fmt.Printf("%d ", i+j) } fmt.Println() }

Solution

  1. Step 1: Identify correct loop ranges

    To print from 1 to 4, loops must run from 1 to 4 inclusive, so conditions use <= 4.
  2. Step 2: Check output format and calculation

    for i := 1; i <= 4; i++ { for j := 1; j <= 4; j++ { fmt.Printf("%d ", i*j) } fmt.Println() } multiplies i and j and prints in one line with spaces, then prints a newline after inner loop, forming a table.
  3. Step 3: Compare other options

    for i := 1; i < 4; i++ { for j := 1; j < 4; j++ { fmt.Println(i*j) } } uses < 4, missing 4. Options C and D add i+j instead of multiplication, which is incorrect for multiplication table.
  4. Final Answer:

    for i := 1; i <= 4; i++ { for j := 1; j <= 4; j++ { fmt.Printf("%d ", i*j) } fmt.Println() } -> Option B
  5. Quick Check:

    Correct ranges and multiplication print = for i := 1; i <= 4; i++ { for j := 1; j <= 4; j++ { fmt.Printf("%d ", i*j) } fmt.Println() } [OK]
Hint: Use <= for inclusive range and multiply counters [OK]
Common Mistakes:
  • Using < instead of <= to miss last number
  • Adding instead of multiplying counters
  • Printing each number on a new line instead of a table
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard