Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to break the outer loop within the inner loop

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading