Skip to content

java break label

cheyiliu edited this page Oct 11, 2014 · 1 revision

break label, jumps to outside of the label block.

  • 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.

demo

个人观点

  • 若破坏了可读性那最好别用, 简单的才是好的。

more reading

Clone this wiki locally