JAVA label and continue, break, java label continue
You can add labels to statement blocks to give them names. The labels are located before the statements. The label can only be referenced by continue and break. The format is as follows:Label: statementOnly one label can be added before the statement, and the label cannot be followed by braces. Use the break label to control the statements in the label. It is usually followed by a loop such as for. while. do-while. By using labels, we can control the external layer cyclically. The following describes how to use continue to control labels.
public class Label { public static void main(String[] args) { System.out.println("i j"); search: for (int i = 0; i < 3; i++) { for (int j = 0; j < 50; j++) { if (j == 3) continue search; System.out.println(i+" "+j); } } }}
The output is as follows:
I j
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
It can be seen that the inner loop has not been executed for 50 times, But when it reaches the continue, it jumps to the outermost loop. After the continue is executed, it executes I ++.
The following is a break label.
public class Label { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("i j"); search: for (int i = 0; i < 3; i++) { for (int j = 0; j < 50; j++) { if (j == 3) break search; System.out.println(i+" "+j); } } }}
The output is as follows:
I j
0 0
0 1
0 2
It can be seen that the inner loop has not been executed 50 times, and the loop has never been executed again after break. Break jumps out of the outermost loop and out of the label range.
The break jump-out label is useful for querying a record. When you find a record you want, you can jump out of the loop without executing it again.
The label statement must be followed in the loop header. Label statements cannot be used before non-cyclic statements.