Go – Loops Overview
Estimated reading: 3 minutes 36 views

🔂 Go Break, Continue, and Goto – Control Your Loop Flow Precisely (2025 Guide)

🧲 Introduction – What Are break, continue, and goto in Go?

Go provides control flow statements like break, continue, and goto to manipulate execution inside loops and code blocks. These are essential tools to exit loops, skip iterations, or jump to specific labels, especially when handling edge cases or simplifying logic.

🎯 In this section, you’ll learn:

  • How and when to use break, continue, and goto
  • Differences between these control statements
  • Practical use cases with labeled examples
  • Best practices and safety tips

break Statement – Exit a Loop Early

The break statement immediately exits the innermost loop (for, switch, or select) where it’s placed.

Example:

for i := 1; i <= 10; i++ {
    if i == 5 {
        break
    }
    fmt.Println(i)
}

📤 Output:

1  
2  
3  
4

✅ Loop exits as soon as i == 5.


continue Statement – Skip to Next Iteration

The continue statement skips the current iteration and moves to the next one.

Example:

for i := 1; i <= 5; i++ {
    if i%2 == 0 {
        continue
    }
    fmt.Println(i)
}

📤 Output:

1  
3  
5

✅ Even numbers are skipped.


goto Statement – Jump to a Label

The goto statement transfers control to a labeled statement within the same function. It’s rarely used but can be helpful to exit nested logic.

Example:

i := 0
Loop:
    fmt.Println(i)
    i++
    if i < 3 {
        goto Loop
    }

📤 Output:

0  
1  
2

⚠️ Avoid overusing goto — it can lead to spaghetti code if misused.


🏷️ Labeled Break and Continue

Use labels to break out of nested loops or skip iterations in outer loops.

🔄 Break Outer Loop:

Outer:
for i := 1; i <= 3; i++ {
    for j := 1; j <= 3; j++ {
        if i*j > 4 {
            break Outer
        }
        fmt.Println(i, j)
    }
}

📤 Output:

1 1  
1 2  
1 3  
2 1

🔄 Continue Outer Loop:

Outer:
for i := 1; i <= 3; i++ {
    for j := 1; j <= 3; j++ {
        if j == 2 {
            continue Outer
        }
        fmt.Println(i, j)
    }
}

📤 Output:

1 1  
2 1  
3 1

continue Outer skips to the next iteration of the outer loop.


🚫 Common Pitfalls

MistakeTip
Using goto excessivelyPrefer structured loops or functions
Using break without a loopOnly valid inside loops, switch, or select
Missing labels in labeled controlLabel must exist and be correctly placed

🧠 Best Practices

  • ✅ Use break to exit loops based on a condition
  • ✅ Use continue to skip logic and avoid deep nesting
  • 🚫 Use goto only for very specific cases, like error recovery
  • ✅ Use labels for clear and readable control across nested loops

📌 Summary – Recap & Next Steps

Control statements like break, continue, and goto help fine-tune loop behavior and exit conditions. They’re especially useful in nested loops, early exits, or skipping over logic.

🔍 Key Takeaways:

  • break exits the current loop or switch immediately
  • continue skips to the next iteration
  • goto jumps to a labeled line in the function
  • Use labels to control nested loops cleanly
  • Avoid overusing goto for better readability

⚙️ Next: Move on to Go Functions to modularize and reuse your logic.


❓ FAQs – Go Break, Continue, and Goto

❓ Can I use break in an if statement?
✅ No. break only works inside for, switch, or select blocks.

❓ How do I skip just one loop inside a nested loop?
✅ Use continue or break without a label—it applies to the innermost loop.

❓ Is goto considered bad in Go?
✅ Not always. It’s discouraged for flow logic, but acceptable in certain scenarios, like error jumps or recovery logic.

❓ Can I break out of multiple loops at once?
✅ Yes. Use a labeled break to exit outer loops directly.

❓ Are labels case-sensitive in Go?
✅ Yes. Labels must be unique, valid identifiers, and are case-sensitive.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go Break / Continue / Goto

Or Copy Link

CONTENTS
Scroll to Top