Here is a less known fact about the "break" statement.
A break statement with label identifier attempts to transfer control out of enclosing labeled block statement. In this case, the break target need not be a while, do, for, or switch statement. If no labeled statement with identifier as its label encloses the break statement, a compile-time error occurs.
(From JLS 14.13)
To further illustrate ....
<pre>
public class
Test {
public static void main (
String [] args ) {
System.out.println ( "Before block" );
block: {
System.out.println ( "Before break" );
if ( args.length == 0 ) {
break block;
}
System.out.println ( "After break" );
}
System.out.println ( "After block" );
}
}
</pre>
Compiles and runs successfully. Note that it doesn't have
any while, do, for, or switch statements.
Results:
>
java Test
Before block
Before break
After block
>java Test 1
Before block
Before break
After break
After block
Regards,
--RajSim
[This message has been edited by maha anna (edited June 01, 2000).]