Golang break and continue: inner loop, outer loop, and select

Golang break and golang continue for go break loop and golang break for loop; golang break outer loop and golang continue outer loop with labels; golang select break exits the select—use a label to leave the surrounding for; continue golang and break vs for and switch.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang break and continue: inner loop, outer loop, and select

break exits the innermost for, switch, or select. continue jumps to the next iteration of the innermost for. That matches searches like go break loop, golang break for loop, golang continue, continue golang, and continue in golang. For golang break outer loop or golang continue outer loop, add a label. For golang select break, remember a bare break leaves the select, not an outer for—use a label on the for when you need both. Prerequisite: for loops in Go; related concurrency: channels and select.

Tested with Go 1.24 on Linux.


golang break for loop (single loop)

go
package main

import "fmt"

func main() {
	for i := 0; i < 10; i++ {
		if i == 4 {
			break
		}
		fmt.Println(i)
	}
	fmt.Println("done")
}
Output

You should print 0 through 3, then done.


Nested loop: inner break only

A break inside the inner for does not stop the outer for:

go
package main

import "fmt"

func main() {
	for i := 1; i < 5; i++ {
		for j := 5; j > 0; j-- {
			if j == i {
				fmt.Printf("outer %d inner %d\n", i, j)
				break
			}
		}
	}
}
Output

You should see four lines as each outer i pairs with inner j.


golang break outer loop (labeled break)

go
package main

import "fmt"

func main() {
outer:
	for i := 0; i < 5; i++ {
		for j := 0; j < 5; j++ {
			if i == 2 && j == 2 {
				fmt.Println("leaving both loops")
				break outer
			}
		}
	}
	fmt.Println("after loops")
}
Output

You should see leaving both loops then after loops.


golang continue and skipping iterations

go
package main

import "fmt"

func main() {
	for i := 1; i < 10; i++ {
		if i%2 != 0 {
			continue
		}
		fmt.Println(i)
	}
}
Output

You should print even numbers 2 through 8.


golang continue outer loop (labeled continue)

go
package main

import "fmt"

func main() {
outer:
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			if j == 1 {
				fmt.Printf("skip rest of inner when i=%d\n", i)
				continue outer
			}
			fmt.Println("i", i, "j", j)
		}
	}
}
Output

You should see i 0 j 0, a skip line for i=0, then similar for i=1 and i=2.


golang select break: leave the select, not the for

Inside a select, break exits the select. To exit an enclosing for, label the for and break that label:

go
package main

import "fmt"

func main() {
	ch := make(chan int, 1)
loop:
	for {
		select {
		case v := <-ch:
			fmt.Println("got", v)
			break loop
		default:
			ch <- 1
		}
	}
	fmt.Println("left for+select")
}
Output

You should see got 1 then left for+select.


switch note: break exits switch, not for

Inside a switch nested in a for, a bare break terminates the switch. Use a label on the for if you need to break out of the loop.


Summary

Go break and continue control the innermost for unless you target switch or select. For go break loop vs golang break outer loop, use labels. For golang continue outer loop, use continue Label. For golang select break, pair select with a labeled for when one case should end the whole loop. Prefer small loops and early return from functions when that reads clearer than deep labels.


References


Frequently Asked Questions

1. How do I golang break outer loop from a nested for?

Put a label on the outer for and use break LabelName; a bare break only exits the innermost for switch or select.

2. Does continue golang skip the inner or outer loop?

By default continue applies to the innermost for; use continue Label on a labeled outer for to skip to the next iteration of that outer loop.

3. What does golang select break do?

A break inside a select terminates that select statement, not the enclosing for; label the for and use break ForLabel inside the select case to exit both.

4. Can I use break or continue inside a switch inside a for?

break exits the switch, not the for; use labels if you need to exit the loop, or restructure with an if inside the for body.

5. Where do break and continue not apply?

They are only meaningful attached to for loops at the grammar level; use return or labeled breaks when you need to leave a function or an arbitrary block.
Antony Shikubu

Systems Integration Engineer

Highly skilled software developer with expertise in Python, Golang, and AWS cloud services.