A for loop in Go has three parts separated by semicolons: initialization, condition, and post statement. It looks like:
for i := 0; i < 5; i++ { // code to repeat }
Click to reveal answer
beginner
How do you write an infinite loop using for in Go?
You write a for loop without any condition or statements:
for { // code runs forever }
Click to reveal answer
beginner
Can a for loop in Go be used like a while loop?
Yes! If you only provide a condition without initialization and post statements, it works like a while loop:
for i < 10 { // code }
Click to reveal answer
beginner
What happens if the condition in a for loop is false at the start?
The loop body does not run at all. The program skips the loop and continues after it.
Click to reveal answer
beginner
How do you stop a for loop early in Go?
Use the break statement inside the loop to exit it immediately.
Click to reveal answer
Which part of the for loop runs first in Go?
APost statement
BCondition check
CLoop body
DInitialization
✗ Incorrect
The initialization runs first, then the condition is checked before running the loop body.
What does this loop do?
for i := 0; i < 3; i++ { fmt.Println(i) }
APrints 0, 1, 2
BPrints 1, 2, 3
CPrints 0, 1, 2, 3
DPrints nothing
✗ Incorrect
The loop starts at 0 and runs while i is less than 3, printing 0, 1, and 2.
How do you write a loop that runs forever in Go?
Afor i := 0; i < 10; i++ {}
Bfor {}
Cwhile true {}
Dloop forever {}
✗ Incorrect
In Go, a bare `for` loop (`for {}`) runs forever.
What keyword stops a for loop immediately?
Abreak
Bexit
Cstop
Dcontinue
✗ Incorrect
break exits the loop immediately. continue skips to the next iteration.
Which of these is a valid for loop condition in Go?
Afor i := 0; i < 5; i++ {}
Bfor (i = 0; i < 5; i++) {}
Cfor i := 0; i < 5; i++ {}
Dfor i < 5; i++ {}
✗ Incorrect
Go uses := for initialization and semicolons to separate parts. Parentheses are not used.
Explain how a for loop works in Go and write a simple example that prints numbers 1 to 5.
Think about how you start, check, and update the loop variable.
You got /5 concepts.
Describe how to create an infinite loop in Go and how to stop it safely.
Remember the simplest for loop form and how to exit loops.
You got /3 concepts.
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
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 D
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
Step 1: Recall Go for loop syntax
Go uses for 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 A
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
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 A
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
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 between i < 5 and i++.
Final Answer:
Missing semicolon between condition and post statement -> Option B
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
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 C
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 unknownstrongwhile earning +1 Grip toward your certificate.