124

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amy B
  • 17,874
  • 12
  • 64
  • 83

12 Answers12

213

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.

SOFe
  • 7,867
  • 4
  • 33
  • 61
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 21
    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 Sep 29 '10 at 13:21
  • 12
    @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. – Sean Patrick Floyd Sep 29 '10 at 13:36
75

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob Di Marco
  • 43,054
  • 9
  • 66
  • 56
31

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
16

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
  • 342,105
  • 78
  • 482
  • 720
5

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codaddict
  • 445,704
  • 82
  • 492
  • 529
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
4

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.

Jay
  • 26,876
  • 10
  • 61
  • 112
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
2

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

SoulWanderer
  • 315
  • 3
  • 11
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vstoyanov
  • 871
  • 5
  • 14
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
greuze
  • 4,250
  • 5
  • 43
  • 62
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RTA
  • 1,251
  • 2
  • 14
  • 33