Loop statements that are prefixed with a label can be the target of break and continue statements. break and continue can accept a single parameter that must be a label of an enclosing loop in the same method and apply to that loop instead of the innermost one.
Rationale: getting rid of the most common use of goto, restricting it to generated jump tables.
Rewritten example from C# reference (goto):
public class NoGotoTest1
{
static void Main()
{
int x = 200, y = 4;
int count = 0;
string[,] array = new string[x, y];
// Initialize the array:
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
array[i, j] = (++count).ToString();
// Read input:
Console.Write("Enter the number to search for: ");
// Input a string:
string myNumber = Console.ReadLine();
// Search:
bool found = false;
Search:
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (array[i, j].Equals(myNumber))
{
found = true;
break Search;
}
}
}
if (found) {
Console.WriteLine("The number {0} is found.", myNumber);
} else {
Console.WriteLine("The number {0} was not found.", myNumber);
}
Console.WriteLine("End of search.");
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
Loop statements that are prefixed with a label can be the target of
breakandcontinuestatements.breakandcontinuecan accept a single parameter that must be a label of an enclosing loop in the same method and apply to that loop instead of the innermost one.Rationale: getting rid of the most common use of
goto, restricting it to generated jump tables.Rewritten example from C# reference (
goto):