-1
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

kostix
  • 51,517
  • 14
  • 93
  • 176
Christopher
  • 39
  • 1
  • 4

1 Answers1

2
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

kostix
  • 51,517
  • 14
  • 93
  • 176
Mahdi zarepoor
  • 310
  • 1
  • 6