Skip to content

[Proposal] Labelled loops as targets for break and continue. #5883

@orthoxerox

Description

@orthoxerox

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();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Language DesignResolution-Won't FixA real bug, but Triage feels that the issue is not impactful enough to spend time on.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions