-->

“loop:” in Java code. What is this, why does it co

2018-12-31 15:44发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

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



回答4:

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


回答5:

Its not a keyword, its a label.

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



回答6:

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



回答7:

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.



回答8:

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



回答9:

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



回答10:

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



回答11:

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



回答12:

it is a label. generaly label used in java to transfer the control flow at desired location while all keyword like continue, break have a specified chice of location.