Loop control break continue
Context
Section titled “Context”Inside loops, break terminates the innermost loop. continue skips to the next iteration.
You can also use labels to break out of nested loops.
Example
Section titled “Example”Use break, continue, and a label.
Code example
Section titled “Code example”package main
import "fmt"
func main() { // continue for i := 0; i < 5; i++ { if i%2 == 0 { continue } fmt.Print(i, " ") } fmt.Println()
// break for i := 0; i < 10; i++ { if i == 3 { break } fmt.Print(i, " ") } fmt.Println()
// label to break out of two loopsouter: for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { if i == 1 && j == 1 { break outer } fmt.Printf("(%d,%d) ", i, j) } } fmt.Println()}Output
Section titled “Output”1 30 1 2(0,0) (0,1) (0,2) (1,0)