C# (Sharp) Programming Language Question:

Is there a way of specifying which block or loop to break out of when working with nested loops?

Tweet Share WhatsApp

Answer:

The easiest way is to use goto: using System;
class BreakExample
{
public static void Main(String[] args)
{
for(int i=0; i<3; i++)
{
Console.WriteLine("Pass {0}: ", i);
for( int j=0 ; j<100 ; j++ )
{
if ( j == 10) goto done;
Console.WriteLine("{0} ", j);
}
Console.WriteLine("This will not print");
}
done:
Console.WriteLine("Loops complete.");
}
}

Download C# (Sharp) Programming Language PDF Read All 163 C# (Sharp) Programming Language Questions
Previous QuestionNext Question
How do I declare inout arguments in C#?What is the difference between const and static read-only?