break和continue的區別

2022-06-29 18:39:07 字數 1072 閱讀 9060

break: 使用在switch-case,迴圈結構中 作用:結束當前迴圈

continue: 使用在迴圈結構中 作用:結束當次迴圈

相同點:後面不能宣告執行語句,如果加了,會直接報錯。

public class breakcontinue01 

system.out.print(i);}}

}//此時break用來結束當前迴圈

//輸出結果為:123

public class breakcontinue01 

system.out.print(i);}}

}//此時continue用來結束當次迴圈

//輸出結果為:1235679

public class breakcontinue02 

system.out.print(j+" ");

}system.out.println();}​

}}/*輸出結果:

1 2 3

1 2 3

1 2 3

*/

public class breakcontinue02 

system.out.print(j+" ");

}system.out.println();}​

}}/*輸出結果:

1 2 3 5 6 7 9 10

1 2 3 5 6 7 9 10

1 2 3 5 6 7 9 10

*/

public class breakcontinue02 

system.out.print(j+" ");

}system.out.println();}​

}}/*輸出結果:

1 2 3

*/

public class breakcontinue02 

system.out.print(j+" ");

}system.out.println();}​

}}/*輸出結果:

1 2 3 1 2 3 1 2 3

*/

break和continue的區別

一 先看msdn關於break和continue語句的幫助說明 1 break 語句用於終止最近的封閉迴圈或它所在的 switch 語句。控制傳遞給終止語句後面的語句 如果有的話 2 continue 語句將控制權傳遞給它所在的封閉迭代語句的下一次迭代。二 對break和continue語句的解釋 ...

break和continue的區別

a for int x 1 x 9 x system.out.println break 跳出 break作用範圍 switch語句或迴圈語句 當break語句單獨存在時,下面不要定義其他語句,因為執行不到 break跳出當前所在迴圈,如果出現了迴圈巢狀,break想要跳出指定的迴圈,可以通過標號來...

break和continue的區別

break是結束整個迴圈體,continue是結束單次迴圈 比方說 1.break while x 10 printf d r n x 結果是輸出 1 2 就退出了整個while迴圈 2.continue while x 10 printf d r n x 結果是 1 2 4 5 6 7 8 9 1...