2

In a conversation earlier this week I was discussing certain language features, and I realized I don't have a good word / phrase to describe a particular feature.

Some languages, such as PHP, have a language construct which allows break and continue statements to accept a numeric parameter indicating which block should be affected. For example:

for ($i = 0; $i < 10; $i++)
    for ($j = 10; $j > 0; $j--)
        if ($j == $i) 
            break 2;
echo $i;

Because break 2; causes the outer loop to break, the output is 1. If this were break 1; or just break; the output would be 10.

C# does not have any such construct. The equivalent would something like this:

for (i = 0; i < 10; i++) {
    bool broken = false;
    for (j = 10; j > 0; j--) {
        if (j == i) {
            broken = true;
            break;
        }
    }
    if (broken)
        break;
}
Console.WriteLine(i);

But of course, this could also be accomplished using a goto.

So my question is, what do you call this? Is there a specific name for this "break n" construct? I'd like to be able to say "Language X has parametric break and continue statements", or something like that.

Sled
  • 1,908
p.s.w.g
  • 4,215

1 Answers1

1

Java has this and it is called a "labelled break" from what I've seen.

From Oracle's Java Tutorial on Branching Statements:

The break statement has two forms: labeled and unlabeled. ... An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement.

In your PHP example it's not really "labelled" per-se (although you could consider the 2 an implicit label) but more like a "relative" or "nested" break. Any of those adjectives should work.


PS - I'm surprised C# didn't steal that from Java since they took just about everything else.

Sled
  • 1,908