GoLang - Break Continue

4 minute read

In this tutorial, you will learn about “Break & Continue in Go Programming” with the help of example.

In Go programming, the “break” and “continue” statements are control flow constructs that allow you to alter the behavior of loops. They provide a way to interrupt the normal flow of execution and control how the program proceeds. In this tutorial, we will explore the concepts of “break” and “continue” in Go programming with examples and explanations in simple and easy-to-understand language. So let’s dive in and learn about “break” and “continue” in Go!

The “break” Statement

The “break” statement is used to immediately exit the innermost loop in which it is placed. It allows you to prematurely terminate the loop’s execution.

Example:

package main

import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        if i == 3 {
            break
        }
        fmt.Println(i)
    }
}

In this example, we have a “for” loop that iterates from 1 to 5. Inside the loop, we have an “if” statement that checks if 'i' is equal to 3. If the condition is true, the “break” statement is executed, and the loop is immediately terminated. As a result, the number 3 is not printed, and the loop exits.

The “continue” Statement

The “continue” statement is used to skip the current iteration of a loop and move to the next iteration. It allows you to bypass the remaining code in the current iteration.

Example:

package main

import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        if i == 3 {
            continue
        }
        fmt.Println(i)
    }
}

In this example, we have a “for” loop that iterates from 1 to 5. Inside the loop, we have an “if” statement that checks if 'i' is equal to 3. If the condition is true, the “continue” statement is executed, and the remaining code in the current iteration is skipped. As a result, the number 3 is not printed, but the loop continues to the next iteration.

Using “break” and “continue” with Nested Loops

Both “break” and “continue” can be used with nested loops to control the flow of execution within multiple levels of loops.

Example with “break”:

package main

import "fmt"

func main() {
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 3; j++ {
            if i == 2 && j == 2 {
                break
            }
            fmt.Printf("(%d, %d) ", i, j)
        }
    }
}

In this example, we have nested “for” loops. The “break” statement is used to exit the inner loop when 'i' is equal to 2 and 'j' is equal to 2. As a result, the loop terminates prematurely, and the program continues with the next iteration of the outer loop.

Example with “continue”:

package main

import "fmt"

func main() {
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 3; j++ {
            if i == 2 && j == 2 {
                continue
            }
            fmt.Printf("(%d, %d) ", i, j)
        }
    }
}

In this example, we have nested “for” loops. The “continue statement is used to skip the current iteration of the inner loop when 'i' is equal to 2 and 'j' is equal to 2. As a result, the remaining code in that iteration is skipped, and the loop proceeds with the next iteration.

Using “break” and “continue” in Switch Statements

“Break” and “continue” can also be used within switch statements to control the flow of execution.

Example with “break”:

package main

import "fmt"

func main() {
    grade := "A"

    switch grade {
    case "A":
        fmt.Println("Excellent!")
        break
    case "B":
        fmt.Println("Good!")
    case "C":
        fmt.Println("Average!")
    default:
        fmt.Println("Need Improvement!")
    }
}

In this example, we have a switch statement that checks the value of the 'grade' variable. When the grade is “A”, the “break” statement is used to exit the switch statement after printing “Excellent!”. Without the “break” statement, the program would continue to evaluate the subsequent cases.

Example with “continue”:

package main

import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        switch i {
        case 2, 4:
            continue
        }
        fmt.Println(i)
    }
}

In this example, we have a “for” loop that iterates from 1 to 5. Inside the loop, we have a switch statement that checks the value of i. When i is 2 or 4, the “continue” statement is used to skip the remaining code in that iteration. As a result, the numbers 2 and 4 are not printed, but the loop continues with the next iteration.

By mastering the usage of “break” and “continue”, you have gained valuable tools to control the flow of your Go programs and handle different scenarios.

The “break” and “continue” statements in Go programming provide control flow mechanisms that allow you to alter the behavior of loops and switch statements. They help in managing program execution and handling specific conditions.

In this tutorial, we explored how to use the “break” statement to immediately exit a loop or switch statement, and the “continue” statement to skip the remaining code in the current iteration and move to the next iteration. We also saw how to use them in the context of nested loops and switch statements.!

Congratulations on completing this beginner’s guide to “break” and “continue” in Go programming! With this knowledge, you can now effectively manage loops and switch statements, making your code more efficient and flexible. So go ahead, practice using “break” and “continue” in your programs, and enhance your control over program execution.

Updated: