0
0
Goprogramming~10 mins

Labelled break and continue in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Labelled break and continue
Start Outer Loop
Start Inner Loop
Check Condition
Labelled break/continue
Exit or Continue
Back to Outer Loop or Exit
The outer loop starts, then the inner loop runs. When a labelled break or continue is hit, control jumps to the labelled loop, either exiting or continuing it.
Execution Sample
Go
Outer:
for i := 1; i <= 3; i++ {
  for j := 1; j <= 3; j++ {
    if j == 2 {
      break Outer
    }
  }
}
This code breaks out of the outer loop when inner loop variable j equals 2.
Execution Table
StepijCondition j==2?ActionLoop State
111NoContinue inner loopInner loop running
212Yesbreak OuterExit outer loop
3----Loops terminated
💡 At step 2, j==2 is true, so break Outer exits the outer loop immediately.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
i11--
j12--
Key Moments - 2 Insights
Why does break Outer exit both loops instead of just the inner loop?
Because the break is labelled with Outer, it tells Go to exit the loop named Outer, not just the current inner loop. See execution_table step 2 where break Outer ends both loops.
What happens if we use continue Outer instead of break Outer?
continue Outer skips the rest of the current iteration of the Outer loop and starts the next iteration. It does not exit the loop. This changes the flow compared to break Outer.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i when break Outer is executed?
A1
B2
C3
D-
💡 Hint
Check the 'i' column at step 2 in the execution_table.
At which step does the program exit all loops?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Action' and 'Loop State' columns in execution_table rows.
If we replace break Outer with continue Outer, what happens at step 2?
AContinue inner loop normally
BExit both loops
CSkip to next iteration of Outer loop
DInfinite loop
💡 Hint
Refer to key_moments explanation about continue Outer behavior.
Concept Snapshot
Labelled break and continue in Go:
- Use labels to name loops: Label:
- break Label exits the named loop immediately.
- continue Label skips to next iteration of named loop.
- Helps control nested loops clearly.
- Syntax: break Label or continue Label
Full Transcript
This example shows how labelled break and continue work in Go. We have an outer loop named Outer and an inner loop inside it. When the inner loop variable j equals 2, break Outer is executed. This causes the program to exit the outer loop immediately, stopping both loops. The execution table traces each step: at step 1, j is 1 and the loop continues; at step 2, j is 2, condition is true, and break Outer exits the loops. The variable tracker shows how i and j change. Key moments clarify why break Outer exits both loops and what continue Outer would do instead. The visual quiz tests understanding of loop variables and control flow. The snapshot summarizes the syntax and behavior of labelled break and continue in Go.