Switch Case with Break in For Loop in Golang

A switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value, also called the case of the expression. There can be various switch-case statements within a switch. Each case is followed by the value to be compared to. When the variable being switched on is equal to its corresponding case, the statements following that case will execute until a break statement is reached. 

When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement, and no other following command in the same case or loop is executed. A switch statement can have an optional default case as well at the last to give the default condition to be executed. The default case can be used for performing a task when none of the cases is true. Let us take an example to understand:



// Golang Program to show the Switch 
// Case with Break in For Loop
package main 
import "fmt"

func main() {
    forLoop:for number := 1; number < 10; number++ {
        fmt.Printf("%d", number)
        switch {
        case number == 1:
            fmt.Println("-- One")
        case number == 2:
            fmt.Println("-- Two")
        case number == 3:
            fmt.Println("-- Three")
        case number == 4:
            fmt.Println("-- Four")
         case number == 5:
            fmt.Println("-- Five")
        case number == 6:
            fmt.Println("-- Six")
        case number > 2:
            fmt.Println("-- Greater than two")
            break forLoop
        case number == 8:
            fmt.Println("-- Eight")
        case number == 9:
            fmt.Println("-- Nine")
            fmt.Println("-- Number not identified")



1-- One
2-- Two
3-- Three
4-- Four
5-- Five
6-- Six
7-- Greater than two

Explanation: Here in the last case before the default case, we have used a break statement that is used for breaking from the forLoop. When numbers are iterated in the loop from 1 to 9, they are being conditionally tested with the switch cases starting from the top. As an example when number = 1 it will print One and so on. Only after the first switch case condition is not satisfied the program checks for the next switch case condition. The numbers are incremented by 1 and as soon as the loop encounters 7 the first matching switch case condition is “number > 2“.

In that condition, we have applied the break statement and as soon as the forLoop encounters this statement, it breaks from the loop without executing the commands after it. As seen, the number 8 is never printed, it is because the loop has a break statement before it. We see that although the numbers are still 9 and also there are conditions for that but they are not executed because the loop encounter break statement before it and thus, the statements after it can not be executed. 

Note: break statement is very important as if you do not apply it, the compiler will throw an error of “label forLoop defined and not used” and the program won’t be executed as a whole even if the numbers will stop incrementing after 10. So, remember to use the break statement carefully.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.