"loop:" in Java code. What is this, and why does it compile?

JavaLabeled Statements

Java Problem Overview


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.

What is the explanation?

Java Solutions


Solution 1 - Java

It is not a keyword it is a label.

Usage:

    label1:
    for (; ; ) {
        label2:
        for (; ; ) {
            if (condition1) {
                // break outer loop
                break label1;
            }
            if (condition2) {
                // break inner loop
                break label2;
            }
            if (condition3) {
                // break inner loop
                break;
            }
        }
    }

[Documentation][1]. [1]: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.7

Solution 2 - Java

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

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

This allows for breaking of the outer loop.

Link to documentation.

Solution 3 - Java

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):

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).

Solution 4 - Java

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
        }
    }
}

Solution 5 - Java

It's not a keyword; it's a label.

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

Solution 6 - Java

It's 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.

It's used on line 148.

Solution 7 - Java

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.

Very late addendum:

I saw a great gag line of code once. The programmer wrote:

http://www.example.com/xyz.jsp
for (Foo foo1 : foolist)

He didn't actually say "example.com" but our company's web site.

It gives the impression that there's a URL in the code. It compiles successfully, like it does something. But ... what does it do?

In reality it does nothing. "http:" is a label that he never references. Then the "//" makes the rest of the line a comment.

Solution 8 - Java

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.

Solution 9 - Java

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

Solution 10 - Java

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:

Thinking in Java, break and continue

Solution 11 - Java

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

Solution 12 - Java

It is a label. Generally a label used in Java to transfer the control flow at desired location while all keywords, like continue and break, have a specified choice of location.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmy BView Question on Stackoverflow
Solution 1 - JavajmjView Answer on Stackoverflow
Solution 2 - JavaRob Di MarcoView Answer on Stackoverflow
Solution 3 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 4 - JavaMichael BorgwardtView Answer on Stackoverflow
Solution 5 - JavacodaddictView Answer on Stackoverflow
Solution 6 - JavaPaulJWilliamsView Answer on Stackoverflow
Solution 7 - JavaJayView Answer on Stackoverflow
Solution 8 - JavaAlberto ZaccagniView Answer on Stackoverflow
Solution 9 - JavaSoulWandererView Answer on Stackoverflow
Solution 10 - JavavstoyanovView Answer on Stackoverflow
Solution 11 - JavagreuzeView Answer on Stackoverflow
Solution 12 - JavaRTAView Answer on Stackoverflow