Continue with the label to continue the loop in java
In java, the break tag can jump out of the specified loop level, and the continue tag can skip certain loops for the next loop.
E.g:
- public class ContinueDemo {
- publicstaticvoid main(String[] args)
- {
- for(int i=0;i<9;i++)
- {
- if(i!=5)
- continue;
- System.out.println("i="+i);
- }
- //-----------------
- out:for(int i=0;i<5;i++)
- {
- System.out.println("i="+i);
- inner:for(int j=0;j<5;j++)
- {
- if(j>=i)
- continue out;
- System.out.println("j="+j);
- }
- }
- }
- }
Among them, continue out is to skip the inner loop and continue the outer loop.
To borrow an example:
- publicclass LabledWhile{
- publicstaticvoid 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;
- }
- }
- }
- }
- }
1. A normal continue will return to the beginning (top) of the innermost loop and continue execution
2. A continue with a label will reach the position of the label and re-enter the loop immediately after that label. The
case from "Top Stories" The break and continue keywords are used with labels to realize the jump function