Go - break statement

The break statement in Go programming language has the following two usages −

  • When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

  • It can be used to terminate a case in a switch statement.

If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.


The syntax for a break statement in Go is as follows −


Flow Diagram

Go break statement


package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 10

   /* for loop execution */
   for a < 20 {
      fmt.Printf("value of a: %d\n", a);
      if a > 15 {
         /* terminate the loop using break statement */

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
Kickstart Your Career

Get certified by completing the course

Get Started