The following simple examples lead to a compile-time error. But it is not clear why.
public static void main (String[] args) throws java.lang.Exception { int i = 0; d: { System.out.println("d"); } while(i < 10){ i++; continue d; } }
- and -
public static void main (String[] args) throws java.lang.Exception { int i = 0; d: { System.out.println("d"); while(i < 10){ i++; continue d; } } }
Demo
But the following works just fine:
public static void main (String[] args) throws java.lang.Exception { int i = 0; d: while(i < 10){ { System.out.println("d"); } i++; continue d; } }
Does it allow passing control to only one while , for or do statement? He does not speak in JLS . In fact, it says:
The continue statement labeled Identifier is trying to transmit (see ยง 14.7) that has the same Identifier as its label; This statement, which is called the continuation of target, immediately terminates the current iteration and starts a new one.
source share