how to break the outer loop within the inner loop

Advertisements
for i := 0; i < 5; i++ {
        fmt.Println("i is", i)
        for j := 0; j < 3; j++ {
            if j == 2 {
                break
            }
            fmt.Println("j is ", j)
        }
    }

I want this code to break the program if the i was equal to 2. how can I signal that I wanna break the outer loop within the inner loop ?

I can’t figure it out how to break or continue the outer loop

>Solution :

outerLoop:
    for i := 0; i < 5; i++ {
        for j := 0; j < 3; j++ {
            if i == 3 {
                break outerLoop
            }
            fmt.Println(i, j)
        }
    }

here is the answer . you can use labels in Go to refer to different loops

Leave a Reply Cancel reply