Using Break and Continue Statements When Working with Loops in Go

Written by Gopher Guides

Using for loops in Go allow you to automate and repeat tasks in an efficient manner.

Learning how to control the operation and flow of loops will allow for customized logic in your program. You can control your loops with the break and continue statements.

Break Statement

In Go, the break statement terminates execution of the current loop. A break is almost always paired with a conditional if statement.

Let’s look at an example that uses the break statement in a for loop:

break.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. for i := 0; i < 10; i++ {
  5. if i == 5 {
  6. fmt.Println("Breaking out of loop")
  7. break // break here
  8. }
  9. fmt.Println("The value of i is", i)
  10. }
  11. fmt.Println("Exiting program")
  12. }

This small program creates a for loop that will iterate while i is less than 10.

Within the for loop, there is an if statement. The if statement tests the condition of i to see if the value is less than 5. If the value of i is not equal to 5, the loop continues and prints out the value of i. If the value of i is equal to 5, the loop will execute the break statement, print that it is Breaking out of loop, and stop executing the loop. At the end of the program we print out Exiting program to signify that we have exited the loop.

When we run this code, our output will be the following:

Output

  1. The value of i is 0
  2. The value of i is 1
  3. The value of i is 2
  4. The value of i is 3
  5. The value of i is 4
  6. Breaking out of loop
  7. Exiting program

This shows that once the integer i is evaluated as equivalent to 5, the loop breaks, as the program is told to do so with the break statement.

Nested Loops

It is important to remember that the break statement will only stop the execution of the inner most loop it is called in. If you have a nested set of loops, you will need a break for each loop if desired.

nested.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. for outer := 0; outer < 5; outer++ {
  5. if outer == 3 {
  6. fmt.Println("Breaking out of outer loop")
  7. break // break here
  8. }
  9. fmt.Println("The value of outer is", outer)
  10. for inner := 0; inner < 5; inner++ {
  11. if inner == 2 {
  12. fmt.Println("Breaking out of inner loop")
  13. break // break here
  14. }
  15. fmt.Println("The value of inner is", inner)
  16. }
  17. }
  18. fmt.Println("Exiting program")
  19. }

In this program, we have two loops. While both loops iterate 5 times, each has a conditional if statement with a break statement. The outer loop will break if the value of outer equals 3. The inner loop will break if the value of inner is 2.

If we run the program, we can see the output:

Output

  1. The value of outer is 0
  2. The value of inner is 0
  3. The value of inner is 1
  4. Breaking out of inner loop
  5. The value of outer is 1
  6. The value of inner is 0
  7. The value of inner is 1
  8. Breaking out of inner loop
  9. The value of outer is 2
  10. The value of inner is 0
  11. The value of inner is 1
  12. Breaking out of inner loop
  13. Breaking out of outer loop
  14. Exiting program

Notice that each time the inner loop breaks, the outer loop does not break. This is because break will only break the inner most loop it is called from.

We have seen how using break will stop the execution of a loop. Next, let’s look at how we can continue the iteration of a loop.

Continue Statement

The continue statement is used when you want to skip the remaining portion of the loop, and return to the top of the loop and continue a new iteration.

As with the break statement, the continue statement is commonly used with a conditional if statement.

Using the same for loop program as in the preceding Break Statement section, we’ll use a continue statement rather than a break statement:

continue.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. for i := 0; i < 10; i++ {
  5. if i == 5 {
  6. fmt.Println("Continuing loop")
  7. continue // break here
  8. }
  9. fmt.Println("The value of i is", i)
  10. }
  11. fmt.Println("Exiting program")
  12. }

The difference in using the continue statement rather than a break statement is that our code will continue despite the disruption when the variable i is evaluated as equivalent to 5. Let’s look at our output:

Output

  1. The value of i is 0
  2. The value of i is 1
  3. The value of i is 2
  4. The value of i is 3
  5. The value of i is 4
  6. Continuing loop
  7. The value of i is 6
  8. The value of i is 7
  9. The value of i is 8
  10. The value of i is 9
  11. Exiting program

Here we see that the line The value of i is 5 never occurs in the output, but the loop continues after that point to print lines for the numbers 6-10 before leaving the loop.

You can use the continue statement to avoid deeply nested conditional code, or to optimize a loop by eliminating frequently occurring cases that you would like to reject.

The continue statement causes a program to skip certain factors that come up within a loop, but then continue through the rest of the loop.

Conclusion

The break and continue statements in Go will allow you to use for loops more effectively in your code.