Bird
Raised Fist0
Goprogramming~20 mins

Why loops are needed in Go - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of a simple for loop in Go
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    for i := 1; i <= 3; i++ {
        fmt.Print(i, " ")
    }
}
A1 2 3
B1 2 3 4
C0 1 2 3
D123
Attempts:
2 left
๐Ÿ’ก Hint
Look at the loop start and end values and what is printed inside the loop.
๐Ÿง  Conceptual
intermediate
1:30remaining
Why use loops instead of repeating code?
Why do programmers use loops instead of writing the same code many times?
ALoops are only used to confuse beginners.
BLoops make the program run slower but look nicer.
CLoops let us repeat actions easily without writing code again and again.
DLoops are used to stop the program from running.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens if you want to do the same thing many times.
โ“ Predict Output
advanced
2:30remaining
Output of nested loops in Go
What does this Go program print?
Go
package main
import "fmt"
func main() {
    for i := 1; i <= 2; i++ {
        for j := 1; j <= 2; j++ {
            fmt.Print(i*j, " ")
        }
    }
}
A1 2 3 4
B1 2 2 4
C1 1 2 2
D2 4 2 4
Attempts:
2 left
๐Ÿ’ก Hint
Multiply i and j for each pair in the nested loops.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in this Go loop
What error does this Go code cause?
Go
package main
import "fmt"
func main() {
    for i := 0; i < 3; i++
        fmt.Println(i)
}
ASyntax error: missing semicolon after for statement
BRuntime error: index out of range
CNo error, prints 0 1 2
DSyntax error: missing braces {} around loop body
Attempts:
2 left
๐Ÿ’ก Hint
Check if the loop body is properly enclosed.
๐Ÿš€ Application
expert
2:30remaining
Count how many times a loop runs
How many times does this loop run?
Go
package main
import "fmt"
func main() {
    count := 0
    for i := 10; i > 0; i -= 3 {
        count++
    }
    fmt.Println(count)
}
A4
B6
C5
D3
Attempts:
2 left
๐Ÿ’ก Hint
Start at 10 and subtract 3 each time until i is no longer greater than 0.

Practice

(1/5)
1. Why do we use loops like for in Go programming?
easy
A. To stop the program from running
B. To store data permanently
C. To create new variables
D. To repeat a set of instructions multiple times without rewriting code

Solution

  1. Step 1: Understand the purpose of loops

    Loops allow repeating actions multiple times without writing the same code again.
  2. Step 2: Identify the correct use case

    Using loops saves time and makes code shorter and easier to manage.
  3. Final Answer:

    To repeat a set of instructions multiple times without rewriting code -> Option D
  4. Quick Check:

    Loops repeat actions = A [OK]
Hint: Loops repeat tasks to avoid rewriting code [OK]
Common Mistakes:
  • Thinking loops store data
  • Confusing loops with variable creation
  • Believing loops stop program execution
2. Which of the following is the correct syntax for a basic for loop in Go?
easy
A. for i := 0; i < 5; i++ { fmt.Println(i) }
B. loop i from 0 to 5 { fmt.Println(i) }
C. while i < 5 { fmt.Println(i) }
D. repeat 5 times { fmt.Println(i) }

Solution

  1. Step 1: Recall Go loop syntax

    Go uses for with initialization, condition, and post statement separated by semicolons.
  2. Step 2: Match syntax to options

    for i := 0; i < 5; i++ { fmt.Println(i) } matches Go's for loop syntax exactly.
  3. Final Answer:

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

    Go uses for with semicolons = C [OK]
Hint: Go loops use for with semicolons, not while or loop [OK]
Common Mistakes:
  • Using while instead of for
  • Using loop keyword which Go doesn't have
  • Missing semicolons in for loop
3. What will be 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. 3 2 1

Solution

  1. 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.
  2. Step 2: Trace output for each iteration

    It prints 1, then 2, then 3, each followed by a space.
  3. Final Answer:

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

    Loop prints 1 to 3 = D [OK]
Hint: Check loop start, end, and increments carefully [OK]
Common Mistakes:
  • Starting loop from 0 instead of 1
  • Including extra numbers beyond 3
  • Printing in reverse order
4. Identify the error in this Go loop code:
for i := 0; i < 5 i++ {
    fmt.Println(i)
}
medium
A. Incorrect variable declaration
B. Using wrong loop keyword
C. Missing semicolon between condition and increment
D. Missing braces around loop body

Solution

  1. Step 1: Check for syntax errors in for loop

    Go requires semicolons separating initialization, condition, and post statements 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 increment -> Option C
  4. Quick Check:

    For loop parts need semicolons = A [OK]
Hint: Remember semicolons separate for loop parts [OK]
Common Mistakes:
  • Omitting semicolons in for loop header
  • Using while instead of for
  • Forgetting braces for loop body
5. You want to print all even numbers from 2 to 10 using a loop in Go. Which code snippet correctly does this?
hard
A. for i := 2; i < 10; i += 2 { fmt.Println(i) }
B. for i := 1; i <= 10; i++ { if i%2 == 0 { fmt.Println(i) } }
C. for i := 2; i < 10; i++ { if i%2 == 0 { fmt.Println(i) } }
D. for i := 2; i <= 10; i++ { fmt.Println(i*2) }

Solution

  1. Step 1: Understand the goal

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

    for i := 1; i <= 10; i++ { if i%2 == 0 { fmt.Println(i) } } -> Option B
  5. Quick Check:

    Check condition inside loop for even numbers = B [OK]
Hint: Use if inside loop to filter even numbers [OK]
Common Mistakes:
  • Forgetting to include 10 in range
  • Incrementing by wrong step
  • Printing wrong multiples
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