Bird
Raised Fist0
Goprogramming~20 mins

Break statement in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Break Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of break in nested loops
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 {
                break
            }
            fmt.Printf("%d%d ", i, j)
        }
    }
}
A11 12 21 22 31 32
B11 21 31
C11 12 21 31
D11 21 22 31
Attempts:
2 left
๐Ÿ’ก Hint
Remember, break exits the innermost loop only.
โ“ Predict Output
intermediate
2:00remaining
Break with label in Go
What is the output of this Go program using a labeled break?
Go
package main
import "fmt"
func main() {
Outer:
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 3; j++ {
            if i*j >= 4 {
                break Outer
            }
            fmt.Printf("%d%d ", i, j)
        }
    }
}
A11 12 13 21 22 23 31 32 33
B11 12 13 21 22 23
C11 12 21 22
D11 12 13 21
Attempts:
2 left
๐Ÿ’ก Hint
A labeled break exits the outer loop entirely.
๐Ÿ”ง Debug
advanced
2:00remaining
Why does this break not stop the loop?
Consider this Go code snippet. Why does the loop not stop when the break statement is reached?
Go
package main
import "fmt"
func main() {
    for i := 0; i < 5; i++ {
        if i == 3 {
            break
        }
        fmt.Println(i)
    }
    fmt.Println("Loop ended")
}
AThe break is inside the if but the condition is never true, so break never runs.
BThe break only exits the inner block, not the loop.
CThe break exits the loop, but the print after the loop runs anyway.
DThe break is misplaced and causes a compile error.
Attempts:
2 left
๐Ÿ’ก Hint
Check what break does and what happens after the loop.
๐Ÿ“ Syntax
advanced
2:00remaining
Identify the syntax error with break
Which option contains a syntax error related to the break statement in Go?
Afor i := 0; i < 5; i++ { break label }
Bfor i := 0; i < 5; i++ { if i == 2 { break } }
Clabel: for i := 0; i < 5; i++ { break label }
Dfor i := 0; i < 5; i++ { if i == 3 { break } }
Attempts:
2 left
๐Ÿ’ก Hint
Check if the label is declared before using break label.
๐Ÿš€ Application
expert
2:00remaining
Count iterations before break with label
How many times does the inner loop print before the program breaks out of both loops?
Go
package main
import "fmt"
func main() {
Outer:
    for i := 1; i <= 4; i++ {
        for j := 1; j <= 4; j++ {
            if i+j > 5 {
                break Outer
            }
            fmt.Printf("%d%d ", i, j)
        }
    }
}
A9
B10
C8
D7
Attempts:
2 left
๐Ÿ’ก Hint
Count all pairs (i,j) where i+j <= 5 before break Outer triggers.

Practice

(1/5)
1. What does the break statement do inside a loop in Go?
easy
A. It pauses the loop for a moment and then continues.
B. It skips the current iteration and moves to the next one.
C. It immediately stops the loop and continues after it.
D. It restarts the loop from the beginning.

Solution

  1. Step 1: Understand the role of break in loops

    The break statement is used to exit a loop immediately, stopping any further iterations.
  2. Step 2: Compare with other loop controls

    Unlike continue which skips to the next iteration, break stops the loop entirely and moves on.
  3. Final Answer:

    It immediately stops the loop and continues after it. -> Option C
  4. Quick Check:

    Break stops loop = It immediately stops the loop and continues after it. [OK]
Hint: Break stops loop immediately, no more repeats [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break pauses the loop
  • Assuming break restarts the loop
2. Which of the following is the correct syntax to use break inside a for loop in Go?
easy
A. for i := 0; i < 5; i++ { if i == 3 { exit } }
B. for i := 0; i < 5; i++ { if i == 3 { stop } }
C. for i := 0; i < 5; i++ { if i == 3 { continue } }
D. for i := 0; i < 5; i++ { if i == 3 { break } }

Solution

  1. Step 1: Identify the correct keyword for stopping a loop

    In Go, break is the keyword used to exit a loop early.
  2. Step 2: Check syntax correctness

    for i := 0; i < 5; i++ { if i == 3 { break } } uses break correctly inside an if statement within the for loop.
  3. Final Answer:

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

    Correct break syntax = for i := 0; i < 5; i++ { if i == 3 { break } } [OK]
Hint: Use 'break' keyword exactly to stop loops [OK]
Common Mistakes:
  • Using 'stop' or 'exit' instead of 'break'
  • Confusing 'continue' with 'break'
  • Missing curly braces around break
3. What will be the output of this Go code?
for i := 1; i <= 5; i++ {
    if i == 3 {
        break
    }
    fmt.Print(i, " ")
}
medium
A. 1 2 3 4 5
B. 1 2
C. 3 4 5
D. 1 2 3

Solution

  1. Step 1: Trace the loop iterations

    The loop starts at i=1 and prints i. When i=3, the break stops the loop immediately.
  2. Step 2: Determine printed values before break

    Values printed before break are 1 and 2, then loop stops before printing 3.
  3. Final Answer:

    1 2 -> Option B
  4. Quick Check:

    Break at 3 stops loop = 1 2 [OK]
Hint: Break stops before printing 3, so only 1 and 2 print [OK]
Common Mistakes:
  • Including 3 in output
  • Printing all numbers ignoring break
  • Confusing break with continue
4. Find the error in this Go code snippet using break:
for i := 0; i < 5; i++ {
    if i == 2 {
        break 2
    }
    fmt.Println(i)
}
medium
A. Using break with a number is invalid in Go.
B. The loop condition is wrong.
C. fmt.Println is not imported.
D. The break statement should be continue.

Solution

  1. Step 1: Understand break usage in Go

    Go's break does not accept a number to break multiple loops; it only breaks the closest loop.
  2. Step 2: Identify invalid syntax

    Using break 2 causes a syntax error because Go does not support labeled breaks with numbers.
  3. Final Answer:

    Using break with a number is invalid in Go. -> Option A
  4. Quick Check:

    Break with number invalid = Using break with a number is invalid in Go. [OK]
Hint: Go break never takes a number, only plain break [OK]
Common Mistakes:
  • Trying to break multiple loops with a number
  • Confusing Go with other languages like JavaScript
  • Ignoring syntax errors from break usage
5. Given nested loops, how can you use break to exit only the inner loop in Go?
for i := 1; i <= 3; i++ {
    for j := 1; j <= 3; j++ {
        if j == 2 {
            break
        }
        fmt.Printf("%d,%d ", i, j)
    }
}

What will be the output?
hard
A. 1,1 2,1 3,1
B. 1,1 1,2 2,1 2,2 3,1 3,2
C. 1,1 1,2 2,1 2,2 3,1 3,2 2,2 3,2
D. 1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3

Solution

  1. Step 1: Understand break in nested loops

    The break inside the inner loop stops only that inner loop when j == 2.
  2. Step 2: Trace output for each iteration

    For each i, inner loop prints when j=1, then breaks at j=2, so output is pairs with j=1 only.
  3. Final Answer:

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

    Break stops inner loop only = 1,1 2,1 3,1 [OK]
Hint: Break stops only closest loop, inner loop ends early [OK]
Common Mistakes:
  • Thinking break exits both loops
  • Expecting all j values printed
  • Confusing break with continue
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