Roslyn: [Proposal] Labelled loops as targets for break and continue.

Created on 12 Oct 2015  路  9Comments  路  Source: dotnet/roslyn

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):

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

}
```

Area-Language Design Resolution-Won't Fix

Most helpful comment

Because the requested change isn't going to happen.

All 9 comments

I often felt the need of a break with label.
An alternate option is introducing a boolean variable, but this introduces an additional check at every iteration.
Otherwise, you can use a goto, but a break label would make things look prettier.

So spelling goto as "break" makes it somehow better? It does exactly the same thing, no?

There's absolutely zero reason to be afraid of using goto statements to break out of loops in C#- they cannot jump between methods and this is their intended use.

Yes but there's still some stigma associated to them
/s

There is a slight semantic difference between break Search; and goto Search;. break would cause the labeled loop to be broken out of, while goto would cause the loop to start over.

The equivalent goto would place the label after the closing brace of the outer for loop.

One possible use for this syntax would be to create a rule disallowing the use of goto if your team feels that is appropriate, while retaining the ability to break out of nested loops or continue from within them.

Yes of course you also have to move the labels. After the loop for break, at its end for continue. It might not be as intuitive as a break or continue.

It's funny, the negative stigma became attached to goto rather than the spaghetti code that it helped to facilitate. Now everyone avoids goto but is more than willing to write a rats nest in its place. In C# specifically I think that the stigma is misplaced as the compiler and CLR put pretty strict rules on where you can jump. It's not like the 50s or 60s when you could jump clear across the program to an arbitrary instruction. Dijkstra's editorial was written four years before c and he was largely repeating Zemanek's arguments from nine years prior to that.

goto is bad,mmmkay

That said, I don't think I've ever found a use for goto outside of explicit switch fall-through, and even that is pretty rare. For your use case I either refactor to a separate method and return once I've found the result I want, or I take advantage of the fact that for allows for multiple conditions:

bool found = false;
for (int i = 0; !found && i < x; i++) {
    for (int j = 0; !found && j < y; j++) {
        if (array[i, j].Equals(myNumber)) {
            found = true;
        }
    }
}

The one thing I don't really like about Java's implementation is that continue label; jumps to after the labelled loop which just looks kind of confusing. At least with goto you are jumping directly to the label.

Rationale: getting rid of the most common use of goto...

We don't have that as a goal.

Why this proposal is closed?

Because the requested change isn't going to happen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

orthoxerox picture orthoxerox  路  3Comments

OndrejPetrzilka picture OndrejPetrzilka  路  3Comments

ashmind picture ashmind  路  3Comments

AdamSpeight2008 picture AdamSpeight2008  路  3Comments

marler8997 picture marler8997  路  3Comments