Bird
Raised Fist0
Goprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a labelled break in Go nested loops?
easy
A. To declare a new variable inside the loop
B. To exit from an outer loop directly
C. To restart the entire program
D. To skip the current iteration of the inner loop

Solution

  1. Step 1: Understand labelled break usage

    A labelled break lets you exit a specific loop, usually an outer one, directly from inside nested loops.
  2. Step 2: Compare with normal break

    Normal break exits only the innermost loop, but labelled break can exit outer loops by specifying the label.
  3. Final Answer:

    To exit from an outer loop directly -> Option B
  4. Quick Check:

    Labelled break = exit outer loop [OK]
Hint: Labelled break jumps out of outer loops directly [OK]
Common Mistakes:
  • Thinking break skips iteration instead of exiting loop
  • Confusing labelled break with continue
  • Assuming break restarts loops
2. Which of the following is the correct syntax to use a labelled continue in Go?
easy
A. continue labelName
B. continue to labelName
C. continue(labelName)
D. continue; labelName

Solution

  1. Step 1: Recall labelled continue syntax

    In Go, to continue a labelled loop, you write continue labelName without parentheses or extra keywords.
  2. Step 2: Check other options

    Options A, B, and D use incorrect syntax like extra words, parentheses, or semicolons which are invalid in Go.
  3. Final Answer:

    continue labelName -> Option A
  4. Quick Check:

    Labelled continue syntax = continue labelName [OK]
Hint: Use 'continue' followed by label name without punctuation [OK]
Common Mistakes:
  • Adding parentheses around label
  • Using extra words like 'to' or semicolons
  • Confusing break and continue syntax
3. What will be the output of the following Go code?
outer:
for i := 1; i <= 3; i++ {
    for j := 1; j <= 3; j++ {
        if i*j > 3 {
            break outer
        }
        fmt.Print(i*j, " ")
    }
}
medium
A. 1 2 3 2
B. 1 2 3 2 4 6 3 6 9
C. 1 2 2 3
D. 1 2 2 3 3

Solution

  1. 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), triggers break outer which exits the outer loop immediately.
  2. Step 2: Understand break outer effect

    Since break outer exits both loops, no further printing occurs after i=2, j=2 check.
  3. Final Answer:

    1 2 3 2 -> Option A
  4. Quick Check:

    Labelled break exits outer loop at i*j>3 [OK]
Hint: Labelled break exits all loops at condition [OK]
Common Mistakes:
  • Ignoring labelled break effect and continuing loops
  • Confusing break with continue
  • Miscomputing i*j values
4. Identify the error in this Go code snippet using labelled continue:
outer:
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        if i == j {
            continue
        }
        fmt.Println(i, j)
    }
}
medium
A. continue cannot be used inside nested loops
B. Label 'outer' is not defined before the loops
C. Missing label after continue to specify which loop to continue
D. fmt.Println syntax is incorrect

Solution

  1. Step 1: Check labelled continue usage

    The code defines label 'outer' but uses plain continue inside inner loop. To continue the outer loop, it must be continue outer.
  2. 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.
  3. Final Answer:

    Missing label after continue to specify which loop to continue -> Option C
  4. Quick Check:

    Labelled continue needs label after continue [OK]
Hint: Use label after continue to affect outer loops [OK]
Common Mistakes:
  • Using continue without label when label is defined
  • Assuming continue skips outer loop without label
  • Confusing label placement
5. Given nested loops labelled outer and inner, how would you skip the rest of the current iteration of the outer loop from inside the inner loop in Go?
hard
A. Use break inner inside the inner loop
B. Use break outer inside the outer loop
C. Use continue inner inside the outer loop
D. Use continue outer inside the inner loop

Solution

  1. Step 1: Understand labelled continue behavior

    Using continue outer inside the inner loop skips the rest of the current iteration of the outer loop and moves to the next iteration of the outer loop.
  2. Step 2: Differentiate from break and other options

    break inner exits the inner loop, but does not skip outer loop iteration. continue inner affects inner loop only. break outer exits the outer loop entirely.
  3. Final Answer:

    Use continue outer inside the inner loop -> Option D
  4. Quick Check:

    Labelled continue skips outer loop iteration [OK]
Hint: Use 'continue' with outer label to skip outer iteration [OK]
Common Mistakes:
  • Using break instead of continue to skip iteration
  • Applying continue to wrong loop label
  • Confusing break and continue effects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard