Golang - Switch Case with Break in For Loop Code Example

The code for Switch Case with Break in For Loop

package main 
import "fmt"

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

/*
Output:
1 : It's One
2 : It's Two
3 : It's Three
4 : It's Four
5 : It's Five
6 : It's Six
7 : It's Greater than two
*/
Code by IncludeHelp, on February 28, 2023 16:03

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.