Challenge - 5 Problems
Labelled Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of labelled break in nested loops
What is the output of this Go program using a labelled break?
Go
package main import "fmt" func main() { OuterLoop: for i := 1; i <= 3; i++ { for j := 1; j <= 3; j++ { if i*j > 3 { break OuterLoop } fmt.Printf("%d,%d ", i, j) } } }
Attempts:
2 left
๐ก Hint
Remember that labelled break exits the outer loop immediately.
โ Incorrect
The inner loop prints pairs until i*j > 3. When i=2 and j=2, 2*2=4 > 3, so break OuterLoop stops all loops. So output stops after printing 1,1 1,2 1,3 2,1.
โ Predict Output
intermediate2:00remaining
Output of labelled continue in nested loops
What is the output of this Go program using a labelled continue?
Go
package main import "fmt" func main() { Outer: for i := 1; i <= 3; i++ { for j := 1; j <= 3; j++ { if i == j { continue Outer } fmt.Printf("%d,%d ", i, j) } } }
Attempts:
2 left
๐ก Hint
Labelled continue skips to the next iteration of the outer loop, skipping remaining inner loop iterations.
โ Incorrect
When i == j, continue Outer skips remaining inner loop iterations and proceeds to the next outer loop iteration (i++). For i=1: skips at j=1 (no output). i=2: prints 2,1, skips at j=2. i=3: prints 3,1 3,2, skips at j=3. Output: 2,1 3,1 3,2
๐ง Debug
advanced2:00remaining
Identify the error with labelled break usage
What error does this Go code produce?
Go
package main func main() { for i := 0; i < 3; i++ { if i == 1 { break Outer } } }
Attempts:
2 left
๐ก Hint
Labels must be declared before use.
โ Incorrect
The label Outer is not declared anywhere, so the compiler reports undefined label error.
โ Predict Output
advanced2:00remaining
Effect of labelled continue on outer loop variable
What is the output of this Go program?
Go
package main import "fmt" func main() { Outer: for i := 1; i <= 2; i++ { for j := 1; j <= 3; j++ { if j == 2 { continue Outer } fmt.Printf("%d,%d ", i, j) } } }
Attempts:
2 left
๐ก Hint
Labelled continue skips to next iteration of outer loop immediately.
โ Incorrect
When j == 2, continue Outer skips the rest of inner loop and increments i. So only j=1 prints for each i.
๐ง Conceptual
expert2:00remaining
Understanding labelled break and continue behavior
Which statement about labelled break and continue in Go is TRUE?
Attempts:
2 left
๐ก Hint
Think about what break and continue do normally and how labels change their target.
โ Incorrect
Labelled break exits the loop with the label; labelled continue skips to the next iteration of that labelled loop. They can be used with for and switch. Unlabelled continue skips current iteration of innermost loop.