java break label - cheyiliu/All-in-One GitHub Wiki
- example 1,
public static void main(String[] args) {
A: {// a
System.out.println("a begin");
B: if (isOK()) {// b
System.out.println("-b begin");
C: if (isOK()) {// c
System.out.println("--c begin");
System.out.println("--c end");
break A;// test this
// break B;// test this
// break C;// test this
}// end c
System.out.println("-b end");
}// end b
System.out.println("a end");
}// end a
}
private static boolean isOK() {
return true;
}
- example 2,
A: while (true) {
B: while (true) {
break A;
}
}
equels to
while (true) {
while (true) {
goto done;
}
}
done:
// Rest of the code here.
- 若破坏了可读性那最好别用, 简单的才是好的。