Bird
Raised Fist0
Goprogramming~10 mins

For loop basics in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - For loop basics
Initialize i=0
↓
Check: i < 5?
No→EXIT
Yes↓
Execute body
↓
Update: i = i+1
↩Back to Check
The loop starts by setting i to 0, then checks if i is less than 5. If yes, it runs the loop body, then increases i by 1 and repeats. If no, it stops.
Execution Sample
Go
for i := 0; i < 5; i++ {
    fmt.Println(i)
}
This code prints numbers from 0 to 4 using a for loop.
Execution Table
IterationiCondition i<5ActionOutput
10truePrint i=0, i++0
21truePrint i=1, i++1
32truePrint i=2, i++2
43truePrint i=3, i++3
54truePrint i=4, i++4
65falseExit loop
💡 i reaches 5, condition 5 < 5 is false, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
i0123455
Key Moments - 3 Insights
Why does the loop stop when i equals 5?
Because the condition i < 5 becomes false at iteration 6, as shown in the execution_table row 6.
Does the loop print the number 5?
No, the loop stops before printing 5 because the condition i < 5 is false when i is 5, as seen in execution_table row 6.
When is the variable i increased?
i is increased after printing in each iteration, before the next condition check, as shown in the Action column of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i during iteration 3?
A2
B3
C1
D0
💡 Hint
Check the 'i' column in execution_table row 3.
At which iteration does the condition i < 5 become false?
A5
B6
C4
D1
💡 Hint
Look at the 'Condition i<5' column in execution_table row 6.
If we change the loop to i < 3, how many times will the loop run?
A2 times
B5 times
C3 times
D4 times
💡 Hint
Compare the condition i < 5 with i < 3 and count iterations accordingly.
Concept Snapshot
Go for loop syntax:
for initialization; condition; post {
    // code
}
Loop runs while condition is true.
Variable updates after each iteration.
Stops when condition is false.
Full Transcript
This visual trace shows a Go for loop starting with i=0. Each step checks if i is less than 5. If true, it prints i and then increases i by 1. This repeats until i reaches 5, when the condition fails and the loop stops. The variable tracker shows i's value changing from 0 up to 5. Key moments clarify why the loop stops before printing 5 and when i increments. The quiz tests understanding of variable values and loop stopping conditions.

Practice

(1/5)
1. What does a for loop do in Go programming?
easy
A. Stops the program immediately
B. Imports a package
C. Defines a new variable
D. Repeats a block of code while a condition is true

Solution

  1. Step 1: Understand the purpose of a for loop

    A for loop is used to repeat code multiple times based on a condition.
  2. 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.
  3. Final Answer:

    Repeats a block of code while a condition is true -> Option D
  4. Quick Check:

    For loop = Repeat code [OK]
Hint: For loops repeat code while condition is true [OK]
Common Mistakes:
  • Thinking for loop stops program
  • Confusing for loop with variable declaration
  • Mixing for loop with import statements
2. Which of the following is the correct syntax for a basic for loop in Go?
easy
A. for i := 0; i < 5; i++ { }
B. for (i = 0; i < 5; i++) { }
C. for i in range(5) { }
D. loop i from 0 to 5 { }

Solution

  1. Step 1: Recall Go for loop syntax

    Go uses for init; condition; post { } syntax without parentheses.
  2. 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.
  3. Final Answer:

    for i := 0; i < 5; i++ { } -> Option A
  4. Quick Check:

    Go for loop = for init; condition; post [OK]
Hint: Go for loops use semicolons, no parentheses [OK]
Common Mistakes:
  • Using parentheses like in C or Java
  • Using Python-style for-in loops
  • Writing 'loop' instead of 'for'
3. What is the output of this Go code?
for i := 1; i <= 3; i++ {
    fmt.Print(i, " ")
}
medium
A. 1 2 3
B. 0 1 2 3
C. 1 2 3 4
D. Syntax error

Solution

  1. 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.
  2. Step 2: Understand print output

    Each iteration prints i followed by a space, so output is "1 2 3 "
  3. Final Answer:

    1 2 3 -> Option A
  4. Quick Check:

    Loop prints 1 to 3 with spaces [OK]
Hint: Check loop start, end, and print inside loop [OK]
Common Mistakes:
  • Starting loop at 0 instead of 1
  • Including extra number beyond 3
  • Confusing print with println
4. Identify the error in this Go for loop:
for i := 0; i < 5 i++ {
    fmt.Println(i)
}
medium
A. Using := instead of = for i
B. Missing semicolon between condition and post statement
C. Using parentheses around for loop
D. Missing curly braces

Solution

  1. Step 1: Check for loop syntax parts

    Go requires semicolons between init, condition, and post parts in for loops.
  2. Step 2: Locate missing semicolon

    The code misses a semicolon between i < 5 and i++.
  3. Final Answer:

    Missing semicolon between condition and post statement -> Option B
  4. Quick Check:

    For loop parts separated by semicolons [OK]
Hint: Check semicolons between for loop parts [OK]
Common Mistakes:
  • Omitting semicolons in for loop header
  • Confusing := with = in loop variable
  • Forgetting curly braces
5. How can you use a for loop in Go to sum all even numbers from 2 to 10 inclusive?
hard
A. for i := 2; i < 10; i += 2 { sum += i }
B. for i := 0; i <= 10; i += 2 { sum += i }
C. for i := 1; i <= 10; i++ { if i%2 == 0 { sum += i } }
D. for i := 2; i < 10; i++ { sum += i*2 }

Solution

  1. Step 1: Understand the goal

    We want to add even numbers from 2 to 10 inclusive.
  2. 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.
  3. Final Answer:

    for i := 1; i <= 10; i++ { if i%2 == 0 { sum += i } } -> Option C
  4. Quick Check:

    Check even with modulo inside loop [OK]
Hint: Use modulo to check even numbers inside loop [OK]
Common Mistakes:
  • Starting loop at 0 including unwanted numbers
  • Multiplying instead of checking evenness
  • Forgetting to check condition inside loop
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