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,868
  • 2
  • 17
  • 24
p.s.w.g
  • 4,135
  • 4
  • 28
  • 40
  • 1
    With php 5.4, the option for this to be a variable was removed. Its no different than a label break in that respect now - just different syntax. Label break is available in all languages that I am familiar with that have a break statement. –  Feb 06 '14 at 20:26
  • @MichaelT Hmm, I wasn't familiar with the "label-break" until you mentioned it. It seems Java supports it ([demo](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/examples/BreakWithLabelDemo.java)), but not C#. – p.s.w.g Feb 06 '14 at 20:32
  • C# does have a `goto` that can be used to skip out of nested loops - see [Breaking out of a nested loop](http://stackoverflow.com/questions/324831/breaking-out-of-a-nested-loop) (and its not a language I'm familiar with) –  Feb 06 '14 at 20:34
  • @MichaelT Just a side note: it seems PHP doesn't support using labels, at least not with the same syntax that Java uses. I tried `outer: for(..) for(..) break outer;` and it appears that `break outer;` is the same as `break;`. But I agree that this is just a variation of the same idea. – p.s.w.g Feb 06 '14 at 20:41
  • 4
    I find `break` with a label name (where the label is, ideally, the *name* of the loop) to be much clearer than `break 2`. Having the *computer* require a *human* to count something is absurd. – Keith Thompson Feb 06 '14 at 20:48
  • This could be reformulated to an inner `while` loop, and I'd be willing to bet that PHP is the *only* language that has this construct. – Robert Harvey Feb 06 '14 at 22:37
  • 1
    @RobertHarvey I know it's rare, but PHP is not the *only* one. After some quick Googling I found [this](https://wci.llnl.gov/codes/basis/manual/node95.html) and [this](http://www.tutorialspoint.com/unix/unix-loop-control.htm), and I seem to remember seeing it elsewhere, too. – p.s.w.g Feb 06 '14 at 22:45
  • Well then. How about "Break n" for a name, then? – Robert Harvey Feb 06 '14 at 22:46
  • @RobertHarvey Yeah, that's what I've been calling it. I was just curious to know if a more formal name existed, and I was hoping to draw on the enormous collective experience of the users on this site. – p.s.w.g Feb 06 '14 at 22:51
  • It's called spaguetti code – Tulains Córdova Feb 07 '14 at 00:25
  • I called it multilevel break in a language I designed. – david.pfx Feb 10 '14 at 14:08
  • @MichaelT: Python doesn't have a labelled break statement. – mipadi Feb 13 '14 at 22:04
  • @mipadi correct, again another language I'm not familiar with. Searching on it, one finds [PEP 3136](http://www.python.org/dev/peps/pep-3136/) which was [rejected](https://mail.python.org/pipermail/python-3000/2007-July/008663.html) - "However, I'm rejecting it on the basis that code so complicated to require this feature is very rare." --- though I don't agree with that. –  Feb 13 '14 at 22:07

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,868
  • 2
  • 17
  • 24