0
0
Goprogramming~20 mins

Break statement in Go - Practice Problems & Coding Challenges

Choose your learning style9 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.