0
0
Goprogramming~20 mins

Labelled break and continue in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Labelled Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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)
    }
}
}
A1,1 1,2 2,1
B1,1 1,2 1,3 2,1
C1,1 1,2 2,1 2,2
D1,1 1,2 1,3 2,1 2,2
Attempts:
2 left
๐Ÿ’ก Hint
Remember that labelled break exits the outer loop immediately.
โ“ Predict Output
intermediate
2: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)
    }
}
}
A2,1 3,1 3,2
B1,2 1,3 2,1 2,3 3,1
C1,2 1,3 2,1 2,3 3,1 3,2 3,3
D1,2 1,3 2,1 2,3 3,1 3,2
Attempts:
2 left
๐Ÿ’ก Hint
Labelled continue skips to the next iteration of the outer loop, skipping remaining inner loop iterations.
๐Ÿ”ง Debug
advanced
2: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
        }
    }
}
Ano error, program runs fine
Bruntime panic: label not found
Ccompile error: undefined label Outer
Dcompile error: break statement not in loop or switch
Attempts:
2 left
๐Ÿ’ก Hint
Labels must be declared before use.
โ“ Predict Output
advanced
2: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)
    }
}
}
A1,1 2,1 2,3
B1,1 2,1 2,2 2,3
C1,1 1,3 2,1 2,3
D1,1 2,1
Attempts:
2 left
๐Ÿ’ก Hint
Labelled continue skips to next iteration of outer loop immediately.
๐Ÿง  Conceptual
expert
2:00remaining
Understanding labelled break and continue behavior
Which statement about labelled break and continue in Go is TRUE?
ALabelled break exits the loop with the matching label; labelled continue skips to the next iteration of the labelled loop.
BLabelled break and continue can only be used with for loops, not switch statements.
CLabelled break skips the current iteration of the labelled loop; labelled continue exits the labelled loop entirely.
DLabelled continue can be used without a label to skip the current iteration of the innermost loop.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what break and continue do normally and how labels change their target.