I suggest you ...

Support labels on loop control statements

Allow break and continue statements to specify which loop they refer to, rather than always binding them to the innermost loop. For example:

FileLoop:
foreach (string fileName in fileList)
{
. . string[] lines = getLines(fileName);

. . for (int lineNum = 0; lineNum < lines.Length; ++lineNum)
. . {
. . . . foreach (string word in splitLineToWords(lines[lineNum]))
. . . . {
. . . . . . if (word == targetWord)
. . . . . . {
. . . . . . . . Console.WriteLine(fileName + " has the target at line " + (lineNum + 1));

. . . . . . . . continue FileLoop;
. . . . . . }
. . . . }
. . }

. . Console.WriteLine(fileName + " doesn't have the target");
}

With C# today, your only option is to break the inner loop after setting some sort of flag to cause the other loops to unwind correctly, and surround the "target not found" message in an if clause. By adding an optional label to loop control statements, everything is much more readable and maintainable.

This wouldn't break existing programs, and since "break" and "continue" are already reserved words, it wouldn't lead to any syntax ambiguities.

7 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Daryl shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    3 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Maksym Nikulyak commented  ·   ·  Flag as inappropriate

        I support this idea; only, to give such loop control statements just enough power, I would allow them to use only the *specialized* (i.e., loop) labels, rather than the existent, general-purpose ones.

        Thinking further of such loop labels, I would suggest that, maybe, they should be made an optional part of the loop syntax - e.g., like this:

        foreach (string fileName in fileList) as MyFileLoop {...}

      • Daryl commented  ·   ·  Flag as inappropriate

        ALL break and continue statements are just different notations of a goto statement. A break is a goto which targets the first statement after the loop block. A continue targets the end of the loop block itself.

        The whole point of loop control statements is that they're easier to understand and maintain than goto's; however you are correct that we could just use goto's everywhere and achieve the same effect. That's why we should allow developers to break or continue a specific loop, rather than only the innermost: to avoid using a goto.

      • dir commented  ·   ·  Flag as inappropriate

        That's just a different notation of a goto label. If you really want (I wouldn't), you can do this right now:

        foreach (string fileName in fileList)
        {
        . . string[] lines = getLines(fileName);

        . . for (int lineNum = 0; lineNum < lines.Length; ++lineNum)
        . . {
        . . . . foreach (string word in splitLineToWords(lines[lineNum]))
        . . . . {
        . . . . . . if (word == targetWord)
        . . . . . . {
        . . . . . . . . Console.WriteLine(fileName + " has the target at line " + (lineNum + 1));

        . . . . . . . . goto FileLoop;
        . . . . . . }
        . . . . }
        . . }

        . . Console.WriteLine(fileName + " doesn't have the target");
        FileLoop:
        }

      Feedback and Knowledge Base