In Java, a label must be used before a loop, a loop that is nested within another loop, continue or break from multiple nesting.
Control loops:
1. The continue will return to the beginning of the most inner loop and continue execution;
2. The tagged continue will reach the position of the label and re-enter the loop under the label;
3. Break breaks and jumps out of the current loop;
4. A tagged break breaks the loop that jumps out of the label.
Example of a label for a for loop
//: Main.java/** * For loop label */Class Main { Public Static void Main(string[] args) {intI=0; Outer for(;true;) {inner: for(; i<Ten; i++) {System.out.println ("i ="+ i);if(i==2) {System.out.println ("Continue");Continue; }if(i==3) {System.out.println ("Break"); ++i; Break; }if(i==7) {System.out.println ("Continue outer"); i++;ContinueOuter }if(i==8) {System.out.println ("Break outer"); BreakOuter } for(intk=0; k<5; k++) {if(k==3) {System.out.println ("Continue inner");ContinueInner } } } } }}/** * output:i = 0 Continue inner i = 1 continue inner i = 2 continue i = 3 break i = 4 continue inner i = 5 continue Inner i = 6 continue inner i = 7 continue outer i = 8 break outer * ///:~
Example of a label for a while loop
//: Main.java/** * While loop tab */Class Main { Public Static void Main(string[] args) {intI=0; Outer while(true) {System.out.println ("Outer while Loop"); while(true) {i++; System.out.println ("i ="+ i);if(i = =1) {System.out.println ("Continue");Continue; }if(i = =3) {System.out.println ("Continue outer");ContinueOuter }if(i = =5) {System.out.println ("Break"); Break; }if(i = =7) {System.out.println ("Break outer"); BreakOuter } } } }}/** * Output:outer while loop i = 1 continue i = 2 I = 3 continue Outer Outer when loop i = 4 i = 5 break Outer Whil E Loop i = 6 i = 7 break outer * ///:~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java-Tags (label)