MacOS System Integrity Protection  • • •  Groundhog Day       all posts in Archive

GOTO in Java

When researching GOTO expression, In Java there’s kind of expression can do similar stuff. It is caused LABELED STATEMENTS . (Read more documents)

    public static void main(String[] args) {

        int[][] arrayOfInts = { { 1, 2 }, { 3, 4 } };

        BREAK_LABLE: for (int i = 0; i < arrayOfInts.length; i++) {
            for (int j = 0; j < arrayOfInts[i].length; j++) {
                if (arrayOfInts[i][j] == 2) {
                    System.out.println("found it");
                    break BREAK_LABLE;
                }
            }
        }
    }

With this expression, we can created much sophisticated loop code as this example:

  label1:
    for (; ; ) {
        label2:
        for (; ; ) {
            if (condition1) {
                // break outer loop
                break label1;
            }
            if (condition2) {
                // break inner loop
                break label2;
            }
            if (condition3) {
                // break inner loop
                break;
            }
        }
    }