Bird
Raised Fist0
Goprogramming~20 mins

Loop execution flow in Go - Practice Problems & Coding Challenges

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 nested loops with continue

What is the output of this Go program?

Go
package main
import "fmt"
func main() {
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 3; j++ {
            if j == 2 {
                continue
            }
            fmt.Printf("%d%d ", i, j)
        }
    }
}
A11 12 13 21 22 23 31 32 33
B11 13 22 23 31 33
C12 13 22 23 32 33
D11 13 21 23 31 33
Attempts:
2 left
๐Ÿ’ก Hint

Remember that continue skips the rest of the current loop iteration.

โ“ Predict Output
intermediate
2:00remaining
Loop with break inside if condition

What will this Go program print?

Go
package main
import "fmt"
func main() {
    for i := 0; i < 5; i++ {
        if i == 3 {
            break
        }
        fmt.Print(i)
    }
}
A0123
B012
C01234
D123
Attempts:
2 left
๐Ÿ’ก Hint

Break exits the loop immediately when the condition is met.

โ“ Predict Output
advanced
2:00remaining
Output of loop with labeled break

What is the output of this Go program?

Go
package main
import "fmt"
func main() {
Outer:
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 3; j++ {
            if i*j > 3 {
                break Outer
            }
            fmt.Printf("%d%d ", i, j)
        }
    }
}
A11 12 21
B11 12 21 22 23
C11 12 21 22
D11 12 13 21 22 23 31 32 33
Attempts:
2 left
๐Ÿ’ก Hint

The labeled break exits the outer loop when the condition is true.

โ“ Predict Output
advanced
2:00remaining
Loop with post statement modifying loop variable

What will this Go program print?

Go
package main
import "fmt"
func main() {
    for i := 0; i < 5; i++ {
        fmt.Print(i)
        i++
    }
}
A024
B01234
C135
D0134
Attempts:
2 left
๐Ÿ’ก Hint

Notice that i++ is called inside the loop body and also in the for statement.

๐Ÿง  Conceptual
expert
2:00remaining
Number of iterations in complex loop

Consider this Go code snippet. How many times will the inner loop's body execute?

for i := 1; i <= 4; i++ {
    for j := i; j <= 4; j++ {
        // loop body
    }
}
A10
B16
C6
D4
Attempts:
2 left
๐Ÿ’ก Hint

Count the number of pairs (i,j) where j >= i and both go up to 4.

Practice

(1/5)
1. What is the main purpose of a for loop in Go?
easy
A. To handle errors
B. To define a new variable
C. To repeat a block of code while a condition is true
D. To declare a function

Solution

  1. Step 1: Understand the role of loops

    Loops are used to repeat actions multiple times based on a condition.
  2. Step 2: Identify the purpose of for in Go

    In Go, the for loop is the only loop and repeats code while a condition holds true.
  3. Final Answer:

    To repeat a block of code while a condition is true -> Option C
  4. Quick Check:

    Loop = repeat code [OK]
Hint: Loops repeat code while condition true [OK]
Common Mistakes:
  • Confusing loops with variable declarations
  • Thinking loops handle errors
  • Mixing loops with function declarations
2. Which of the following is the correct syntax for a basic for loop in Go that runs 3 times?
easy
A. for i := 0; i < 3; i++
B. for (i = 0; i < 3; i++) { }
C. for i = 0; i < 3; i++ { }
D. for i := 0; i < 3; i++ { }

Solution

  1. Step 1: Check Go loop syntax

    Go requires initialization with :=, condition, and post statement inside parentheses without extra parentheses around the whole for.
  2. Step 2: Validate each option

    for i := 0; i < 3; i++ { } uses correct syntax: for i := 0; i < 3; i++ { }. Options B and C have wrong parentheses or missing :=. for i := 0; i < 3; i++ misses braces.
  3. Final Answer:

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

    Correct Go for loop syntax = for i := 0; i < 3; i++ { } [OK]
Hint: Use := for init, semicolons, braces for loop [OK]
Common Mistakes:
  • Using parentheses like C/Java
  • Missing := for variable declaration
  • Omitting braces {}
3. What will be the output of this Go code?
for i := 1; i <= 3; i++ {
    if i == 2 {
        continue
    }
    fmt.Print(i)
}
medium
A. 13
B. 12
C. 123
D. 23

Solution

  1. Step 1: Understand the loop and continue

    The loop runs i from 1 to 3. When i equals 2, continue skips printing.
  2. Step 2: Trace output for each i

    i=1 prints '1', i=2 skips print, i=3 prints '3'. Output is '13'.
  3. Final Answer:

    13 -> Option A
  4. Quick Check:

    Continue skips i=2 print = 13 [OK]
Hint: Continue skips current iteration print [OK]
Common Mistakes:
  • Assuming continue prints the skipped value
  • Confusing continue with break
  • Ignoring loop condition
4. Identify the error in this Go loop code:
for i := 0; i < 5; i++
    fmt.Println(i)
medium
A. Missing braces {} around loop body
B. Incorrect loop condition
C. Variable i not declared
D. Using println instead of Println

Solution

  1. Step 1: Check Go loop syntax rules

    Go requires braces {} to define the loop body even if it is one statement.
  2. Step 2: Identify missing braces

    The code lacks braces around fmt.Println(i), causing a syntax error.
  3. Final Answer:

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

    Braces required for loop body [OK]
Hint: Always use braces {} for loop body [OK]
Common Mistakes:
  • Omitting braces for single statement
  • Misreading error as variable issue
  • Confusing println with Println
5. Given this Go code, what will be the output?
sum := 0
for i := 1; i <= 5; i++ {
    if i%2 == 0 {
        continue
    }
    if sum > 5 {
        break
    }
    sum += i
}
fmt.Println(sum)
hard
A. 6
B. 9
C. 10
D. 5

Solution

  1. Step 1: Trace loop with continue and break

    Loop i=1 to 5, skip even i (2,4). Add odd i to sum until sum > 5, then break.
  2. Step 2: Calculate sum step-by-step

    i=1 (odd): sum=0+1=1
    i=2 (even): continue, skip
    i=3 (odd): sum=1+3=4
    i=4 (even): continue, skip
    i=5 (odd): sum=4+5=9 but before adding, sum=4 which is not >5, so add 5 sum=9. Now sum >5, break after this iteration.
  3. Step 3: Check when break occurs

    Break happens after sum=9, but condition checks sum >5 before adding i=5, so sum was 4 before adding 5. Actually, break triggers only if sum >5 before adding i.
  4. Step 4: Correct calculation

    At i=5, sum=4 (not >5), so add 5 sum=9. Next iteration would be i=6 (loop ends). So break never triggers because sum >5 check is before adding i.
  5. Final Answer:

    9 -> Option B
  6. Quick Check:

    Sum after adding odd numbers until break = 9 [OK]
Hint: Check continue and break order carefully [OK]
Common Mistakes:
  • Adding after break condition incorrectly
  • Ignoring continue skipping even numbers
  • Misunderstanding break timing
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