File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes loops & labels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "loops & labels" Watch "loops & labels" New topic
Author

loops & labels

Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
What will be the result of attempting to compile and run the following program?

Am I allowed to use labels anywhere inside code..loop or no loop?
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Anupreet,
I think your program will fail compilation because the label i2 is proceeded by the variable declaration of j. Labels must immediately follow a block of codes like those enclosed by for-loop, while, or just simply {}.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
According to JLS 17.7, any statement may have a label prefix.
Identifier : Statement
Now, it is important to know what is Not a Statement.
int i = 1; // Not a Statement
label1 : i = 2;
label2 : i++;
label3 : {}
one: two: three: if (i > 0) break two; else i++;
As far as I can tell, local variable declarations and local class declarations are not Statements.
void m() {
label1 : int i = 1; // No!
label2 : class C {} // No!
}
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Marlene Miller:
According to JLS 17.7, any statement may have a label prefix.

That's interesting.
In that case, this is also allowed:
label1:;
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: loops & labels