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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,866 questions

51,788 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 157 views
3 answers 198 views
198 views asked Sep 7, 2022 by avibootz
1 answer 219 views
3 answers 215 views
3 answers 172 views
3 answers 185 views
2 answers 175 views
...