Challenge - 5 Problems
Break Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2: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) } } }
Attempts:
2 left
๐ก Hint
Remember, break exits the innermost loop only.
โ Incorrect
The break statement stops the inner loop when j == 2, so only j=1 prints for each i. Output is 11 21 31.
โ Predict Output
intermediate2: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) } } }
Attempts:
2 left
๐ก Hint
A labeled break exits the outer loop entirely.
โ Incorrect
When i*j >= 4, break Outer exits both loops. The first time this happens is at i=2, j=2 (2*2=4). So output is 11 12 13 21.
๐ง Debug
advanced2: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") }
Attempts:
2 left
๐ก Hint
Check what break does and what happens after the loop.
โ Incorrect
The break stops the loop when i == 3, so the loop ends early. The print after the loop runs normally.
๐ Syntax
advanced2:00remaining
Identify the syntax error with break
Which option contains a syntax error related to the break statement in Go?
Attempts:
2 left
๐ก Hint
Check if the label is declared before using break label.
โ Incorrect
Option A uses break label without declaring label, causing a compile error.
๐ Application
expert2: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) } } }
Attempts:
2 left
๐ก Hint
Count all pairs (i,j) where i+j <= 5 before break Outer triggers.
โ Incorrect
Pairs printed are (1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(3,1),(3,2). Total 9 prints before break.