Recall & Review
beginner
What does the
break statement do in Go?The
break statement immediately stops the execution of the nearest enclosing loop or switch statement and exits it.Click to reveal answer
beginner
In which Go control structures can you use the
break statement?You can use
break inside for loops and switch statements to exit them early.Click to reveal answer
intermediate
What happens if you use
break inside a nested loop?The
break statement only exits the innermost loop where it is used, not all outer loops.Click to reveal answer
advanced
How can you use
break with labels in Go?You can label loops and use
break with the label name to exit an outer loop from inside an inner loop.Click to reveal answer
beginner
Example: What will this code print?
for i := 1; i <= 5; i++ {
if i == 3 {
break
}
fmt.Println(i)
}It will print:<br>1<br>2<br>Because when
i becomes 3, the break stops the loop immediately.Click to reveal answer
What does the
break statement do inside a for loop in Go?✗ Incorrect
The
break statement stops the loop immediately and exits it.Can
break be used to exit a switch statement in Go?✗ Incorrect
In Go,
break can be used to exit a switch statement early.What happens if you use
break inside an inner loop nested in an outer loop?✗ Incorrect
The
break stops only the innermost loop where it is used.How do you break out of an outer loop from inside an inner loop in Go?
✗ Incorrect
You label the outer loop and use
break with that label to exit it.What will this code print?
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if j == 1 {
break
}
fmt.Print(j)
}
fmt.Println(i)
}✗ Incorrect
The inner loop prints 0 then breaks when j == 1, then the outer loop prints i. So output is 001 012.
Explain how the
break statement works in Go loops and switches.Think about how to stop a repeating action early.
You got /3 concepts.
Describe how to use labels with
break to exit outer loops in Go.Labels help break out of nested loops.
You got /3 concepts.