0Pricing
Go Academy · Lesson

break, continue and Labels

Loop control flow and labeled breaks

break, continue and Labels is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Loop Control Fundamentals

Go provides three keywords for controlling loop execution:

  • break — exits the current loop or switch
  • continue — skips to the next iteration
  • goto — jumps to a labeled statement (rarely used)

Labels extend break and continue to work with outer loops.

break in a Loop

break immediately exits the innermost for, switch, or select statement:

for i := 0; i < 100; i++ {
    if i*i > 50 {
        break
    }
    fmt.Println(i)
}

continue

continue skips the rest of the current loop body and jumps to the post statement (or condition check):

for i := 0; i < 10; i++ {
    if i%2 == 0 {
        continue // skip even numbers
    }
    fmt.Println(i) // prints 1,3,5,7,9
}

Labeled break

A label before a for loop lets you break out of it from an inner loop:

search:
for i, row := range grid {
    for j, cell := range row {
        if cell == target {
            fmt.Printf("found at %d,%d\n", i, j)
            break search
        }
    }
}

Labeled continue

Labeled continue skips to the next iteration of the labeled outer loop:

outer:
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        if j == 1 {
            continue outer
        }
        fmt.Printf("%d,%d\n", i, j)
    }
}

When to Use Labels

Labels are appropriate when:

  • You need to break out of a nested loop from deep inside
  • A function extraction would be too verbose

Labels are uncommon in Go — if you find yourself needing them often, consider refactoring with functions or channels.

goto (Rare)

Go has goto for jumping to a labeled statement. It's allowed within the same function and cannot jump over variable declarations:

  • Use cases: generated code, tight error-handling loops
  • Avoid in hand-written code — prefer structured control flow

break in select

Inside a select statement, break exits the select — not an outer for loop. Use a labeled break to exit the outer loop:

loop:
for {
    select {
    case msg := <-ch:
        handle(msg)
    case <-done:
        break loop
    }
}

Avoid Deep Nesting

Deeply nested loops are hard to read. Before adding a labeled break, ask:

  • Can I extract the inner loop into a helper function?
  • Can I use a channel or slice to simplify the iteration?
  • Can early returns reduce nesting at the outer level?

Practical Pattern: Search with break

A common pattern: search a collection and break when found:

func contains(slice []string, target string) bool {
    for _, v := range slice {
        if v == target {
            return true // prefer return over break+flag
        }
    }
    return false
}

Quick Check

How do you exit an outer loop from inside an inner loop in Go?

Recap: break, continue and Labels

Control loop flow effectively in Go:

  • break exits the current loop or switch
  • continue skips to the next iteration
  • Labels let break/continue target an outer loop
  • goto exists but is rarely used in idiomatic Go
  • Inside select, use a labeled break to exit an outer for loop

Next course: Functions and Multiple Return Values.

Frequently asked questions

Is the “break, continue and Labels” lesson free?

Yes — the full text of “break, continue and Labels” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “break, continue and Labels”?

Loop control flow and labeled breaks You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “break, continue and Labels” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. if Statements and Init Expressions
  2. The for Loop: All Patterns
  3. switch Statements in Go
  4. break, continue and Labels
← Back to Go Academy