Golang break keyword
last modified May 7, 2025
This tutorial explains how to use the break
keyword in Go. We'll
cover loop control basics with practical examples of breaking out of loops.
The break statement terminates execution of the innermost loop or switch statement. It's used to exit loops prematurely when a certain condition is met.
In Go, break
can be used in for loops, switch statements, and with
labels for more complex control flow. It helps optimize code by avoiding
unnecessary iterations.
Basic break in a for loop
The simplest use of break
exits a loop when a condition is met.
This example demonstrates breaking out of an infinite loop.
package main import "fmt" func main() { i := 0 for { fmt.Println(i) i++ if i > 5 { break } } fmt.Println("Loop exited") }
The infinite loop runs until i
exceeds 5. The break
statement terminates the loop, and execution continues after the loop block.
Breaking a nested loop
break
only affects the innermost loop. This example shows how it
works in nested loop structures.
package main import "fmt" func main() { for i := 0; i < 3; i++ { fmt.Printf("Outer loop iteration %d\n", i) for j := 0; j < 5; j++ { fmt.Printf(" Inner loop iteration %d\n", j) if j == 2 { break } } } }
The inner loop breaks when j
reaches 2, but the outer loop
continues normally. Each outer iteration restarts the inner loop.
Break in a switch statement
In switch statements, break
exits the switch block. This example
demonstrates its use in case clauses.
package main import "fmt" func main() { num := 2 switch num { case 1: fmt.Println("One") break fmt.Println("This won't print") case 2: fmt.Println("Two") case 3: fmt.Println("Three") } fmt.Println("Switch completed") }
The first case shows explicit break
, while others rely on implicit
termination. The statement after break
is never executed.
Labeled break statement
Go supports labeled breaks to exit outer loops. This powerful feature helps control complex nested loops.
package main import "fmt" func main() { OuterLoop: for i := 0; i < 5; i++ { for j := 0; j < 5; j++ { fmt.Printf("i=%d, j=%d\n", i, j) if i == 2 && j == 2 { break OuterLoop } } } fmt.Println("Both loops exited") }
The label OuterLoop:
marks the outer loop. When executed,
break OuterLoop
exits both loops immediately.
Break in a for-range loop
break
works similarly in range loops. This example processes a
slice until a condition is met.
package main import "fmt" func main() { fruits := []string{"apple", "banana", "cherry", "date", "elderberry"} for index, fruit := range fruits { fmt.Printf("%d: %s\n", index, fruit) if fruit == "cherry" { fmt.Println("Found cherry, stopping") break } } }
The loop iterates through the slice until "cherry" is found. The break
stops further processing of remaining elements.
Practical example: Search loop
This practical example demonstrates using break
in a search
algorithm to exit early when the target is found.
package main import "fmt" func main() { numbers := []int{45, 23, 67, 89, 12, 56, 34} target := 12 found := false for _, num := range numbers { if num == target { found = true break } } if found { fmt.Printf("Found target %d\n", target) } else { fmt.Printf("Target %d not found\n", target) } }
The loop breaks immediately when the target number is found, avoiding unnecessary iterations through the remaining elements.
Break vs continue vs return
This example contrasts break
with other control flow statements
to clarify their differences.
package main import "fmt" func main() { fmt.Println("Break example:") for i := 0; i < 5; i++ { if i == 3 { break } fmt.Println(i) } fmt.Println("\nContinue example:") for i := 0; i < 5; i++ { if i == 3 { continue } fmt.Println(i) } fmt.Println("\nReturn example:") for i := 0; i < 5; i++ { if i == 3 { return } fmt.Println(i) } fmt.Println("This won't print due to return") }
break
exits the loop, continue
skips to next iteration,
and return
exits the entire function. Each serves different purposes.
Source
This tutorial covered the break
keyword in Go with practical
examples of loop control in various scenarios.
Author
List all Golang tutorials.