Recall & Review
beginner
What is a labelled break in Go?
A labelled break in Go is used to exit an outer loop from inside an inner loop by specifying the label name of the outer loop.
Click to reveal answer
beginner
How does labelled continue differ from a normal continue in Go?
Labelled continue skips the current iteration of the loop identified by the label, which can be an outer loop, while normal continue only affects the innermost loop.
Click to reveal answer
beginner
Show the syntax of a labelled break in Go.
labelName:
for condition {
for condition {
if someCondition {
break labelName
}
}
}
Click to reveal answer
intermediate
Why use labelled break or continue instead of normal break or continue?
They allow you to control outer loops directly from inner loops, making it easier to manage complex nested loops without extra flags or complicated logic.
Click to reveal answer
beginner
Can you use labelled break or continue without defining a label?
No, labelled break and continue require a label to specify which loop to affect. Without a label, break and continue only affect the innermost loop.
Click to reveal answer
What does a labelled break do in Go?
✗ Incorrect
A labelled break exits the loop identified by the label, which can be an outer loop.
Which keyword is used to skip to the next iteration of a labelled loop in Go?
✗ Incorrect
The continue keyword, when used with a label, skips to the next iteration of the labelled loop.
What happens if you use break without a label inside nested loops?
✗ Incorrect
A break without a label only exits the innermost loop.
Is this valid Go code?
outer:
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if j == 1 {
continue outer
}
}
}
✗ Incorrect
In Go, continue can be used with a label to skip to the next iteration of the labelled loop.
What is the purpose of labels in Go loops?
✗ Incorrect
Labels name loops so break and continue can refer to outer loops.
Explain how labelled break works in Go with an example.
Think about how to stop an outer loop from inside an inner loop.
You got /4 concepts.
Describe the difference between normal continue and labelled continue in Go.
Consider which loop each continue affects.
You got /4 concepts.