The break statement stops a loop or switch early. It helps you exit when you find what you want or need to stop.
Break statement in Go
Introduction
Syntax
Go
breakThe break statement has no extra words or symbols.
It works inside loops (for) and switch statements.
Examples
Go
for i := 0; i < 10; i++ { if i == 5 { break } fmt.Println(i) }
Go
switch day := "Monday"; day { case "Saturday", "Sunday": fmt.Println("Weekend") break case "Monday": fmt.Println("Start of week") }
Sample Program
This program prints numbers from 1 to 5. When i reaches 6, the break stops the loop.
Go
package main import "fmt" func main() { for i := 1; i <= 10; i++ { if i == 6 { break } fmt.Println(i) } }
Important Notes
In Go, break only stops the innermost loop or switch.
Unlike some languages, Go switch cases do not fall through by default, so break is often not needed there.
Use break carefully to avoid skipping important code unintentionally.
Summary
The break statement stops loops or switch cases early.
It helps control when to exit repeating actions.
In Go, break is simple and only affects the closest loop or switch.
Practice
1. What does the
break statement do inside a loop in Go?easy
Solution
Step 1: Understand the role of break in loops
Thebreakstatement is used to exit a loop immediately, stopping any further iterations.Step 2: Compare with other loop controls
Unlikecontinuewhich skips to the next iteration,breakstops the loop entirely and moves on.Final Answer:
It immediately stops the loop and continues after it. -> Option CQuick Check:
Break stops loop = It immediately stops the loop and continues after it. [OK]
Hint: Break stops loop immediately, no more repeats [OK]
Common Mistakes:
- Confusing break with continue
- Thinking break pauses the loop
- Assuming break restarts the loop
2. Which of the following is the correct syntax to use
break inside a for loop in Go?easy
Solution
Step 1: Identify the correct keyword for stopping a loop
In Go,breakis the keyword used to exit a loop early.Step 2: Check syntax correctness
for i := 0; i < 5; i++ { if i == 3 { break } } usesbreakcorrectly inside anifstatement within theforloop.Final Answer:
for i := 0; i < 5; i++ { if i == 3 { break } } -> Option DQuick Check:
Correct break syntax = for i := 0; i < 5; i++ { if i == 3 { break } } [OK]
Hint: Use 'break' keyword exactly to stop loops [OK]
Common Mistakes:
- Using 'stop' or 'exit' instead of 'break'
- Confusing 'continue' with 'break'
- Missing curly braces around break
3. What will be the output of this Go code?
for i := 1; i <= 5; i++ {
if i == 3 {
break
}
fmt.Print(i, " ")
}medium
Solution
Step 1: Trace the loop iterations
The loop starts at i=1 and prints i. When i=3, thebreakstops the loop immediately.Step 2: Determine printed values before break
Values printed before break are 1 and 2, then loop stops before printing 3.Final Answer:
1 2 -> Option BQuick Check:
Break at 3 stops loop = 1 2 [OK]
Hint: Break stops before printing 3, so only 1 and 2 print [OK]
Common Mistakes:
- Including 3 in output
- Printing all numbers ignoring break
- Confusing break with continue
4. Find the error in this Go code snippet using
break:for i := 0; i < 5; i++ {
if i == 2 {
break 2
}
fmt.Println(i)
}medium
Solution
Step 1: Understand break usage in Go
Go'sbreakdoes not accept a number to break multiple loops; it only breaks the closest loop.Step 2: Identify invalid syntax
Usingbreak 2causes a syntax error because Go does not support labeled breaks with numbers.Final Answer:
Using break with a number is invalid in Go. -> Option AQuick Check:
Break with number invalid = Using break with a number is invalid in Go. [OK]
Hint: Go break never takes a number, only plain break [OK]
Common Mistakes:
- Trying to break multiple loops with a number
- Confusing Go with other languages like JavaScript
- Ignoring syntax errors from break usage
5. Given nested loops, how can you use
What will be the output?
break to exit only the inner loop in Go?for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if j == 2 {
break
}
fmt.Printf("%d,%d ", i, j)
}
}What will be the output?
hard
Solution
Step 1: Understand break in nested loops
Thebreakinside the inner loop stops only that inner loop whenj == 2.Step 2: Trace output for each iteration
For each i, inner loop prints when j=1, then breaks at j=2, so output is pairs with j=1 only.Final Answer:
1,1 2,1 3,1 -> Option AQuick Check:
Break stops inner loop only = 1,1 2,1 3,1 [OK]
Hint: Break stops only closest loop, inner loop ends early [OK]
Common Mistakes:
- Thinking break exits both loops
- Expecting all j values printed
- Confusing break with continue
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
