1. 程式人生 > >淺析break與continue區別

淺析break與continue區別

//break是結束整個迴圈體,continue是結束單次迴圈

比方說:

while(x++ < 10)
{
if(x == 3)
{
break;
}
printf("%d\r\n", x);
}
結果是輸出 1 2 就退出了整個while迴圈

但是如果使用continue
while(x++ < 10)
{
if(x == 3)
{
continue;
}
printf("%d\r\n", x);
}
結果是:1 2 4 5 6 7 8 9 10 可見他僅僅是不輸出3,因為他結束了本次迴圈

其實說白了就是打麻將的時候前者就是掀桌子不繼續打了,後者就是當時那局沒法出輸贏中斷不打了,下局繼續打。