package tw.leonchen.myproject.flowcontrol.loop;
/*
 * break  label
 */
public class TestBreakLabel 
{
 public static void main(String[] args)
 {
test:for(int i=1;i<=5;i++){
 for(int j=1;j<=5;j++){
 if(i==2 && j==3){
 break test ; 
//test是個標籤,當break test時
//返回至test標籤處,之後整個test區塊不執行而跳過
//所以這個程式System.out.println("i="+i+" j="+j);不會被執行。    
 
// break;
//如果break沒加標籤test就只是跳離當層迴圈(j),回到i迴圈+1從i=3開始繼續執行
 }
 System.out.println("i="+i+" j="+j);  
 }
}
   System.out.println("finished");
 }
}
沒有留言:
張貼留言