129

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?

0

12 Answers 12

222

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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.

Comments

32

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

2 Comments

FYI you don't need the loop, a ; in the next line is enough
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)
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 } } } 

3 Comments

inner label is useless here, break; is enough
If there are two ways to break the loop, I appreciate the inner label for clarity.
@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.
5

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

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

Comments

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.

Comments

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.

Comments

2

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

Comments

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.

Comments

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

Comments

1

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.