A new product!

Social Media Toolkit

Download our all in one automation tool for various social media websites.

Gopher

Break Statement in Golang

Published on 29 March 2020
Last Updated on 29 March 2020

In Golang, a break statement can be used for terminating execution of innermost for, switch or select statement, after that it transfers the execution of program to the next statements following the break statement.

In this article, we will demonstrate all the possible ways by which we can make use of the break statement.

Program to demonstrate use of break keyword inside a for loop

Program given below will demonstrate how a break keyword can be used inside a for loop for terminating its execution when a certain condition is met.

// example to demonstrate use of break keyword for terminating a for loop
package main

import "fmt"

func main() {
	for a := 0; a < 100; a++ {
		fmt.Println("inside for loop")
		fmt.Println("a is:", a)

		// check whether a is now 3
		if a == 3 {
			// if a is 3 then break the loop
			break
		}
	}

	fmt.Println("outside for loop")
}

Program output

Above program produces following output:

inside for loop
a is: 0
inside for loop
a is: 1
inside for loop
a is: 2
inside for loop
a is: 3
outside for loop

Program Description

Inside for loop. It checks whether value of variable a has become 3. If this condition is true then break statement is executed. Once break statement is executed the execution of innermost for loop is immediately terminated and program flow resumes at the statement outside the for loop.

How to use label and break statements with for loops

Program given below will demonstrate how to label a specific loop and terminate that loop itself using a labelled break statement.

// example to demonstrate use of break keyword for terminating a for loop
package main

import "fmt"

func main() {
	// label for outer loop
outer:
	for a := 0; a < 3; a++ {
		if a == 1 {
			fmt.Println("break outer called")
            // labelled break statement
			break outer
		}
		// label for inner loop
	inner:
		for b := 0; b < 3; b++ {
			if b == 1 {
				fmt.Println("break inner called")
                // labelled break statement
				break inner
			}
		}
	}
	fmt.Println("outside loop")
}

Program output

Above program produces following output:

break inner called
break outer called
outside loop

Program Description

Normally, when a break statement is called, it halts the execution of the innermost loop and not the outer loop. When multiple loops have been nested inside one another then you may want to terminate the execution of a specific loop by. This can be done by labelling a specific loop and then calling break statement using the label.

Above program makes use of a labelled break statement to halt a specific labeled loop from executing its code.

Program to demonstrate break statement in switch case

Given below is a program that demonstrates how to use a break keyword in a switch case statement.

// example to demonstrate use of break keyword in switch statement

package main

import "fmt"

func main() {
	var a = 1
	switch a {
	case 1:
		fmt.Println("1")
		break
		fmt.Println("hello")
	case 2:
		fmt.Println("2")
	}
}

Program output

Above program produces following output:

1

Program Description

As displayed in above program, break statement can be used inside a matched case statement to immediately halt execution of a switch case statement.