Java:label的使用(for迴圈)

來源:互聯網
上載者:User

標籤:

 1 package java_test; 2  3 public class LabeledFor { 4  5     public static void main(String[]args){ 6         int i=0; 7         outer: //Cannot have statement here 8             for(;true;){ 9                 inner:10                     for(;i<10;i++){11                         System.out.println("i= "+i);12                         if(i==2){13                             System.out.println("continue");14                             continue;15                         }16                         if(i==3){17                             System.out.println("break");18                             i++; //Otherwise i never gets incremented19                             break;20                         }21                         if(i==7){22                             System.out.println("continue outer");23                             i++;24                             continue outer;25                         }26                         if(i==8){27                             System.out.println("break outer");28                             break outer;29                         }30                         for(int k=0;k<5;k++){31                             if(k==3){32                                 System.out.println("continue inner");33                                 continue inner;34                             }35                         }36                     }37             }38             //Cannot break or continue to labels here39     }40 }

輸出

 1 i= 0 2 continue inner 3 i= 1 4 continue inner 5 i= 2 6 continue 7 i= 3 8 break 9 i= 410 continue inner11 i= 512 continue inner13 i= 614 continue inner15 i= 716 continue outer17 i= 818 break outer

用法:

 1 label1: 2     outer-iteration{ 3     inner-iteration{ 4         //... 5         break;//(1) 6         //... 7         continue;//(2) 8         //... 9         continue label1;//(3)10         //...11         break label1;//(4)12     }13 }

In (1),the break breaks out of the inner iteration and you end up in the out iteration.

in(2), the continue moves back to the beginning of the inner iteration.

But in (3),the continue label1 breaks out of the inner iteration and the outer iteration,all the way back to label1. Then it does in fact continue the iteration,but starting at the outer iteration.

In(4),the break label1 also breaks all the way out to label1,but it does not reenter the iteration.It actually does break out of both iterations.

Java:label的使用(for迴圈)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.