Labelled break and continue in Go - Time & Space Complexity
When using labelled break and continue in Go, it is important to understand how these statements affect the flow of loops and the total steps the program takes.
We want to see how the number of operations changes as the input size grows when these labels control loop exits or skips.
Analyze the time complexity of the following code snippet.
outer:
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if j == i {
continue outer
}
// some constant time work
}
}
This code uses a labelled continue to jump to the next iteration of the outer loop when a condition is met inside the inner loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops with an inner loop that may break early using labelled continue.
- How many times: Outer loop runs n times; inner loop runs fewer than n times each outer iteration because of the labelled continue.
Because the inner loop stops early when j equals i, the total work is less than n x n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 45 inner loop steps |
| 100 | About 4950 inner loop steps |
| 1000 | About 499,500 inner loop steps |
Pattern observation: The total steps grow roughly like the sum of numbers from 1 to n, which grows faster than n but slower than n squared.
Time Complexity: O(n²)
This means the total steps grow roughly proportional to the square of the input size, even with the labelled continue.
[X] Wrong: "Labelled continue makes the loops run in linear time because it skips inner iterations."
[OK] Correct: The inner loop still runs many times overall; skipping some iterations early does not reduce the total work enough to change the overall growth from quadratic.
Understanding how labelled break and continue affect loop execution helps you reason about code efficiency and control flow clearly, a skill valuable in many programming tasks.
What if we replaced the labelled continue with a labelled break? How would the time complexity change?
Practice
break in Go nested loops?Solution
Step 1: Understand labelled break usage
A labelled break lets you exit a specific loop, usually an outer one, directly from inside nested loops.Step 2: Compare with normal break
Normal break exits only the innermost loop, but labelled break can exit outer loops by specifying the label.Final Answer:
To exit from an outer loop directly -> Option BQuick Check:
Labelled break = exit outer loop [OK]
- Thinking break skips iteration instead of exiting loop
- Confusing labelled break with continue
- Assuming break restarts loops
Solution
Step 1: Recall labelled continue syntax
In Go, to continue a labelled loop, you writecontinue labelNamewithout parentheses or extra keywords.Step 2: Check other options
Options A, B, and D use incorrect syntax like extra words, parentheses, or semicolons which are invalid in Go.Final Answer:
continue labelName -> Option AQuick Check:
Labelled continue syntax = continue labelName [OK]
- Adding parentheses around label
- Using extra words like 'to' or semicolons
- Confusing break and continue syntax
outer:
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if i*j > 3 {
break outer
}
fmt.Print(i*j, " ")
}
}
Solution
Step 1: Trace the nested loops and condition
For i=1, j=1 to 3: i*j = 1,2,3 (all ≤ 3), so prints "1 2 3 ". For i=2, j=1: i*j=2 (≤3), prints "2 ". Next j=2: i*j=4 (>3), triggersbreak outerwhich exits the outer loop immediately.Step 2: Understand break outer effect
Since break outer exits both loops, no further printing occurs after i=2, j=2 check.Final Answer:
1 2 3 2 -> Option AQuick Check:
Labelled break exits outer loop at i*j>3 [OK]
- Ignoring labelled break effect and continuing loops
- Confusing break with continue
- Miscomputing i*j values
outer:
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if i == j {
continue
}
fmt.Println(i, j)
}
}Solution
Step 1: Check labelled continue usage
The code defines label 'outer' but uses plaincontinueinside inner loop. To continue the outer loop, it must becontinue outer.Step 2: Understand effect of missing label
Without label, continue only skips current inner loop iteration, not the outer loop. The label is unused, which is likely a mistake.Final Answer:
Missing label after continue to specify which loop to continue -> Option CQuick Check:
Labelled continue needs label after continue [OK]
- Using continue without label when label is defined
- Assuming continue skips outer loop without label
- Confusing label placement
outer and inner, how would you skip the rest of the current iteration of the outer loop from inside the inner loop in Go?Solution
Step 1: Understand labelled continue behavior
Usingcontinue outerinside the inner loop skips the rest of the current iteration of the outer loop and moves to the next iteration of the outer loop.Step 2: Differentiate from break and other options
break innerexits the inner loop, but does not skip outer loop iteration.continue inneraffects inner loop only.break outerexits the outer loop entirely.Final Answer:
Use continue outer inside the inner loop -> Option DQuick Check:
Labelled continue skips outer loop iteration [OK]
- Using break instead of continue to skip iteration
- Applying continue to wrong loop label
- Confusing break and continue effects
Ready to prove it?
Pass the test and this pattern moves unknown
