Knowledge Point: continue and break use tags to implement the jump function
Although goto is a reserved word in Java, It is not used in the language, and Java does not have Goto. Then, Java can complete some jump-like operations, that is, use break and continue together with tags.
A tag is followed by an identifier with a colon, as shown below:
Lable1:
In Java,The only place where a tag works is before the iteration statement.. "Just before" means that it is not good to place any statement between the tag and iteration. The only reason for setting labels before iteration is: we want to nest another iteration or switch in it. This is because the break and continue keywords usually only interrupt the current loop, but if they are used with tags, they will interrupt the loop until the tag is located:
The following is an example of a tag for a For Loop:
public class LabledFor{ public static void main(String[] args){ int i = 0; outer: for(;true;){ inner: for(;i<10;i++){ System.out.println("i="+i); /**在抵达for循环的末尾之前,递增表达式会执行*/ if(i==2){ System.out.println("continue"); continue; } /**break会中断for循环,而且在抵达for循环的末尾之前,递增表达式不会执行。由于 * break跳过了增量表达式,所以在此处直接对i进行递增操作*/ if(i==3){ System.out.println("break"); i++; break; } /**continue outer语句会跳到循环顶部,而且也会跳过inner标签下的for循环的递增, * 所以这里也对i直接递增*/ if(i==7){ System.out.println("continue outer"); i++; continue outer; } if(i==8){ System.out.println("break outer"); break outer; } /**在抵达inner标签下的for循环末尾之前,inner下的for循环的递增表达式会执行*/ for(int k = 0;k< 5;k++){ if(k==3){ System.out.println("continue inner"); continue inner; } } } } }}
Output:
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
Without the break outer statement, there is no way to jump out of the External Loop from the internal loop. This is because the break itself can only interrupt the innermost loop (the same is true for continue ).
The following example shows the usage of the break with tags and the continue statement in the while loop:
public class LabledWhile{ public static void main(String[] args){ int i = 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"); continue outer; } if(i==5){ System.out.println("break"); break; } if(i==7){ System.out.println("break outer"); break outer; } } } }}
Output:
Outer WHILE LOOP
I = 1
Continue
I = 2
I = 3
Continue outer
Outer WHILE LOOP
I = 4
I = 5
Break
Outer WHILE LOOP
I = 6
I = 7
Break outer
The same rule applies to the while:
1. The general continue will return to the beginning (top) of the innermost loop and continue execution.
2. The continue with tags will reach the tag location and re-enter the loop next to the tag
3. Generally, the break is interrupted and jumps out of the current loop.
4. The labelled break will be interrupted and jump out of the loop indicated by the tag.
Use the break and continue keywords with tags to implement the jump function