Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Labels ......

 
meeta verma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why this gives a compilation error? Is continue to be used only with for and while.
(code tags added)

[ May 24, 2004: Message edited by: Barry Gaunt ]
[ May 24, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the compilation error is:


And the Java Language Specification says:

If no while, do, or for statement of the immediately enclosing method or initializer block encloses the continue statement, a compile-time error occurs.

[ May 24, 2004: Message edited by: Barry Gaunt ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in other words, labels can ONLY be used in conjunction with loops.



is therefore illegal code in Java.



is legal if not exactly good coding style
 
Wendal Park
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, using labels referred to loops is legal in Java. You may hence think it's not the usual coding style. However, for labels that are used in nested loops scenario, it may give you a better idea about the true value of using labels. It gives an alternative to manipulating a couple of indices in a program (which make code less readable).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic