1

Here you can find that at its initial phases Java provided the goto instruction. Wisely, this instruction is not longer implemented. Nevertheless, as I discovered few minutes ago in other SO question, labels do still exist.

int x;
letsassign: x = 3;

I wonder why they are still there. What other uses do labels have apart from goto label?

Community
  • 1
  • 1
  • 2
    `break label` and `continue label`, mostly. – awksp Jun 24 '14 at 13:04
  • @user3580294 Isn't that as nasty as `goto`? – Pablo Francisco Pérez Hidalgo Jun 24 '14 at 13:05
  • Hard to say. Depends on whether you think `goto` is bad in the first place, I suppose. From what I hear they can be useful for nested loops. Never had to use them myself though. I don't think they're bad. – awksp Jun 24 '14 at 13:08
  • 3
    Say you're 5 loops deep in a nested loop. Would you rather check a conditional variable 5 times in order to escape, or use a `break label`? – alexgerst Jun 24 '14 at 13:09
  • @alexgerst You are right. It is an use, I would try to simplify that code anyway but it is true that for very concrete cases it may be needed. – Pablo Francisco Pérez Hidalgo Jun 24 '14 at 13:14
  • 1
    @user3580294 I think it is bad after having read a lot of VB6 code where the flying spaghetti monster seemed to had possessed the code. – Pablo Francisco Pérez Hidalgo Jun 24 '14 at 13:16
  • @PabloFranciscoPérezHidalgo I suppose that'd just be my lack of experience speaking, then. I'll take your word for it. – awksp Jun 24 '14 at 13:25
  • @PabloFranciscoPérezHidalgo I agree that labels and gotos definitely have the potential for horrendously unreadable code, especially if the programmer thinks of them as the primary tool for control flow. However, judiciously used, they are not always evil. See [this question](http://stackoverflow.com/q/24451/1552305). – alexgerst Jun 24 '14 at 13:33

1 Answers1

4

It is for sportly multiple loopings:

outer: for (;;) {
    while (true) {
        if (brr) {
            break outer;
        }
    }
    roo();
}

For break and continue.

Java was designed to clean things up; after C++. However one did not want to risk outcries of "I cannot longer do that efficiently."

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138