You can give the statement block designator a name that precedes the statement. Labels can only be referenced by continue and break. The format is as follows:
label:statement
Only one label is allowed before the statement, and the label cannot be followed by curly braces. The statement in the label is controlled by adding a label after break. Often the label is for.while.do-while and other loops.by marking, we can control the outer layer of the loop.The following is the control label with continue
Public classLabel { Public Static voidMain (string[] args) {System.out.println ("I J"); Search: for(inti = 0; I < 3; i++) { for(intj = 0; J < 50; J + +) { if(J = = 3) Continuesearch; 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 is not executed 50 times, and to the continue to jump to the outer loop, continue execution after the execution is i++.
Here is the break control label
Public classLabel { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println ("I J"); Search: for(inti = 0; I < 3; i++) { for(intj = 0; J < 50; J + +) { if(J = = 3) Breaksearch; 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 is not executed 50 times, and the loop does not execute after break. Break jumps out of the outermost loop and jumps out of the label's range.
Break out of the label is useful for querying a record, when you find a record you want, you can jump out of the loop, no more execution.
The label statement must be immediately on the head of the loop. The label statement cannot be used in front of a non-circular statement.
Java designator and Continue,break