📖 Scenario: Imagine you are managing a small factory with multiple production lines. Each line produces different products, and you want to check the quality of each product. If a product fails quality check, you want to stop checking that line and move to the next one. Also, if a product is perfect, you want to skip some extra checks and continue with the next product.
🎯 Goal: You will write a Go program that uses labelled break and continue statements to control loops over production lines and products. This will help you understand how to jump out of or skip iterations in nested loops.
📋 What You'll Learn
Create a nested loop to iterate over production lines and products
Use a labelled break to exit the outer loop when a product fails quality
Use a labelled continue to skip extra checks for perfect products
Print messages showing the flow of checks and loop control
💡 Why This Matters
🌍 Real World
In factories or software testing, sometimes you need to stop checking a group when a problem is found or skip unnecessary steps for perfect items.
💼 Career
Understanding labelled break and continue helps in writing clear and efficient code for nested loops, common in data processing, simulations, and automation tasks.
Progress0 / 4 steps
1
Create production lines and products
Create a variable called lines which is a slice of slices of strings. It should have exactly two production lines: the first line has products "A1", "A2", and the second line has products "B1", "B2".
Go
Hint
Use a slice of slices of strings to represent lines and their products.
2
Add quality status for products
Create a map called quality with string keys and string values. It should have these entries: "A1": "perfect", "A2": "fail", "B1": "ok", "B2": "perfect".
Go
Hint
Use a map to store the quality status of each product.
3
Use labelled break and continue in nested loops
Write nested for loops to iterate over lines and their products. Use a label LineCheck for the outer loop. Inside the inner loop, if the product's quality is "fail", use break LineCheck to stop checking all lines. If the quality is "perfect", print "Skipping extra checks for [product]" and use continue to skip to the next product. Otherwise, print "Checking product [product]".
Go
Hint
Use a label before the outer loop and refer to it in the break statement. Use continue inside the inner loop to skip to the next product.
4
Print the final output
Add a print statement after the loops that prints exactly "Quality check complete." to show the process ended.
Go
Hint
Use fmt.Println("Quality check complete.") after the loops.
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
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 B
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
Step 1: Recall labelled continue syntax
In Go, to continue a labelled loop, you write continue labelName without 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.
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
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.
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 A
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
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.
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 C
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
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.
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.
Final Answer:
Use continue outer inside the inner loop -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.