Java break [flag]; 與continue [flag]; 區別

來源:互聯網
上載者:User

標籤:

break [flag];
直接結束掉到標記(flag)層的迴圈
NO1:for(int i=0; i<100; i++){
    for(int j=0; j<10; j++){
      sum = i*j;
      if(sum>500){
        System.out.println(i+"*"+j+"="+sum);
        break NO1;
      }
    }
  }
例如:當sum>500時退出兩層for迴圈;

continue [flag];
退出標記層的本次迴圈
NO1:for(int i=0; i<100; i++){
     for(int j=0; j<10; j++){
      sum = i*j;
      if(sum>500){
        System.out.println(i+"*"+j+"="+sum);
        continue NO1;
      }
    }
  }
例如:當i=72, j=7 sum>500時, 直接跳出當i=72的情況,(則不會執行i=72 ,j=8 和i=72 , j= 9的情況)直接從i=73的迴圈開始
輸入:
56*9=504
57*9=513
58*9=522
59*9=531
60*9=540
61*9=549
62*9=558
63*8=504
64*8=512
65*8=520
66*8=528
67*8=536
68*8=544
69*8=552
70*8=560
71*8=568
72*7=504
73*7=511
74*7=518
75*7=525
76*7=532
77*7=539
78*7=546
79*7=553
80*7=560
81*7=567
82*7=574
83*7=581
84*6=504
85*6=510
86*6=516
87*6=522
88*6=528
89*6=534
90*6=540
91*6=546
92*6=552
93*6=558
94*6=564
95*6=570
96*6=576
97*6=582
98*6=588
99*6=594

Java break [flag]; 與continue [flag]; 區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.