Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,787 answers

573 users

How to use goto to break out from nested for loop in C#

2 Answers

0 votes
using System;
 
class Program
{
    static void Main() {
        int[,] arr = new int[3, 4];
 
        for (int i = 0; i < arr.GetLength(0); i++) {
            for (int j = 0; j < arr.GetLength(1); j++) {
                arr[i, j] = (i + 1) * j;
                Console.WriteLine(arr[i, j]);
            }
        }
 
        for (int i = 0; i < arr.GetLength(0); i++) {
            for (int j = 0; j < arr.GetLength(1); j++) {
                if (arr[i, j] == 6) {
                    goto Found;
                }
            }
        }
         
        Console.WriteLine("Not found");
        goto Finish;
 
    Found:
        Console.WriteLine("Found");
 
    Finish:
        Console.WriteLine("End of program");
    }
}
 
 
 
/*
run:
 
0
1
2
3
0
2
4
6
0
3
6
9
Found
End of program
 
*/

 





answered Nov 25, 2020 by avibootz
edited Nov 25, 2020 by avibootz
0 votes
using System;
 
class Program
{
    static void Main() {
        int[,] arr = new int[3, 4];
 
        for (int i = 0; i < arr.GetLength(0); i++) {
            for (int j = 0; j < arr.GetLength(1); j++) {
                arr[i, j] = (i + 1) * j;
                Console.WriteLine(arr[i, j]);
            }
        }
 
        for (int i = 0; i < arr.GetLength(0); i++) {
            for (int j = 0; j < arr.GetLength(1); j++) {
                if (arr[i, j] == 99) {
                    goto Found;
                }
            }
        }
         
        Console.WriteLine("Not found");
        goto Finish;
 
    Found:
        Console.WriteLine("Found");
 
    Finish:
        Console.WriteLine("End of program");
    }
}
 
 
 
/*
run:
 
0
1
2
3
0
2
4
6
0
3
6
9
Not found
End of program
 
*/

 





answered Nov 25, 2020 by avibootz
edited Nov 25, 2020 by avibootz

Related questions

2 answers 73 views
3 answers 74 views
1 answer 117 views
3 answers 69 views
...