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
Joined: Aug 03, 2002
Posts: 7729
posted
0
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.
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
42
Wendal Park
Ranch Hand
Joined: Aug 28, 2001
Posts: 39
posted
0
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).