Continue with the label to continue the loop in java

Continue with the label to continue the loop in java

Reprinted  at 23:30:02 on February 20, 2016
  • 1328

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:

[java]  view plain copy  
  1. public class  ContinueDemo {   
  2.     publicstaticvoid main(String[] args)    
  3.     {  
  4.         for(int i=0;i<9;i++)  
  5.         {  
  6.             if(i!=5)  
  7.                 continue;  
  8.             System.out.println("i="+i);  
  9.         }  
  10.         //-----------------  
  11.         out:for(int i=0;i<5;i++)  
  12.         {  
  13.             System.out.println("i="+i);  
  14.             inner:for(int j=0;j<5;j++)  
  15.             {  
  16.                 if(j>=i)  
  17.                     continue out;  
  18.                 System.out.println("j="+j);  
  19.             }  
  20.         }  
  21.     }  
  22. }  

Among them, continue out is to skip the inner loop and continue the outer loop.

To borrow an example:

[java]  view plain copy  
  1. publicclass LabledWhile{   
  2.     publicstaticvoid main(String[] args){    
  3.         int i = 0;  
  4.         outer:  
  5.             while(true){  
  6.                 System.out.println("Outer while loop");  
  7.                 while(true){  
  8.                     i++;  
  9.                     System.out.println("i="+i);  
  10.                     if(i==1){  
  11.                         System.out.println("continue");  
  12.                         continue;  
  13.                     }  
  14.                     if(i==3){  
  15.                         System.out.println("continue outer");  
  16.                         continue outer;  
  17.                     }  
  18.                     if(i==5){  
  19.                         System.out.println("break");  
  20.                         break;  
  21.                     }  
  22.                     if(i==7){  
  23.                         System.out.println("break outer");  
  24.                         break outer;  
  25.                     }  
  26.                 }  
  27.             }  
  28.     }  
  29. }  

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949032&siteId=291194637