views:

1504

answers:

11

This code just made me stare at my screen for a few minutes:

loop:
for (;;) {
    // ...
}

(line 137 here)

I have never seen this before, and I had no idea Java has a "loop" keyword (Netbeans doesn't even color it like a keyword), and it does compile fine with JDK 6.

Can someone explain this to me?

+52  A: 

It is not a keyword it is a label

Doc

Usage:

label1:
for(){
  label2:
  for(){
      if(condition1)
      break label1;//break outerloop

      if(condition2)
      break label2;//break innerloop
  }
}
org.life.java
TIL about labels. Thanks.
Coronatus
@Coronatus : You should accept his answer if it helped you. ;)
Silence
Worth mentioning, as the OP might not be familiar with the goto/label concept, that it's generally regarded as a bad practice, except in very particular situations (for instance, for shortcutting multiple nested loops).
haylem
@haylem yeah true
org.life.java
@haylem true, but I would go further: multiple nested loops are usually a bad practice, too. Whenever I used loop labels, I eventually refactored the code so I didn't need them. There is always a better way than that.
seanizer
@Silence - I was going to, but SO made me wait :) Now accepted and upvoted.
Coronatus
@seanizer: indeed, good addendum.
haylem
TIL: how to screw with the violent psychopath that ends up maintaining my code and knows where I live.
+1  A: 

Its not a keyword, its a label.

It allows you to go a labeled break and labeled continue.

codaddict
+2  A: 

Its a break point label, to allow you to break out of a specified loop, rather than simply the innermost one you happen to be in.

Its used on line 148

Visage
A: 

It is a label, and labels in java can be used with the break and continue key words for additional control over loops. Here it is explained in a rather good way: http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ305_024.htm

vstoyanov
+47  A: 

As other posters have said, it is a label, not a key word. Using labels allows you to do things like

outer: for(;;) {
   inner: for(;;) {
     break outer;
   }
}

This allows for breaking of the outer loop.

EDIT: With link to documentation

Rob Di Marco
+1 for showing a practical use.
f1sh
I'd upvote if you included the same link org.life.java did.
DJClayworth
+1  A: 

It's a label, though look at the following example:

int a = 0;
int b = 0
while (a<10){
    firstLoop:
    a++;
    while(true){
        b++
        if(b>10){
            break firstLoop;
        }
    }
 }

When b>10 the execution flow goes to the outer loop

Alberto Zaccagni
+12  A: 

That's not a keyword, it's a label. It's meant to be used with the break and continue keywords inside nested loops:

outer:
for(;;){
    inner:
    for(;;){
        if(){
            break inner; // ends inner loop
        } else {
            break outer; // ends outer loop
        }
    }
}
Michael Borgwardt
also +1 for practical use!
f1sh
`inner` label is useless here, `break;` is enough
gertas
If there are two ways to break the loop, I appreciate the `inner` label for clarity.
Steve Jackson
@gertas: I think he's just demonstrating the point. But per Steve Jackson, it might be a good idea to say it for self-documentation purposes anyway.
Jay
A: 

You could write almost anything, as it is a label... You have an example here

SoulWanderer
+1  A: 

It is not a keyword, but a label. If inside the for loop you write break loop;, you exits that loop

greuze
+8  A: 

The question is answered, but as a side note:

I have heard of interview questions a la "Why is this java code valid" (stripped the simpler example, here's the meaner one, thx Tim Büthe):

<!-- this line is just there to turn java syntax
     highlighting off, please ignore it, the real
     code starts below -->

url: http://www.myserver.com/myfile.mp3
downLoad(url);

Would you all know what this code is (apart from awful)?

Solution: two labels, url and http, a comment www.myserver.com/myfile.mp3 and a method call with a parameter that has the same name (url) as the label. Yup, this compiles (if you define the method call and the local variable elsewhere).

seanizer
FYI you don't need the loop, a ; in the next line is enough
Tim Büthe
Thanks for reminding me, I think there were no loops in the question. I'll edit my answer (I was never asked this question but the interviewer was a former colleague of mine)
seanizer
hah! seanizer! was just going to +1 this anyways and then i saw it's you. i'm not stalking you, honest! :)
Epaga
Well I won't complain if you stalk me with upvotes :-)
seanizer
+1  A: 

This is really a reply to seanizer's comment on org.life.java's answer, but I wanted to put in some code so I couldn't use the comment feature.

While it is very rare that I find a use for "break label", it does happen occassionally. The most common case is when I am searching for something that is in a structure requiring a nested loop to search, like:

search:
for (State state : stateList)
{
  for (City city : state.cityList)
  {
    if (city.zipcode.equals(wantZip))
    {
      doSomethingTo(city);
      break search;
    }
  }
}

Usually in such cases I push the whole thing into a subroutine so that on a hit I can return the found object, and if it falls out the bottom of the loop I can return null to indicate a not found, or maybe throw an exception. But this is occasionally useful.

Frankly, I think the inventors of Java included this feature because between this and exception handling, they eliminated the last two legitimate uses for GOTO.

Jay