break 與 continue
break:
break語句在while,for, do...while或者switch語句中執行時,立刻使程序控制退出這些語句。程序繼續勢利這些語句之後的第一條語句。break的常見用法是要麼提前離開循環,要麼用於跳過switch語句的剩餘部分。
continue:
continue在while, for, do...while語句執行中,使程序跳過循環體內剩下的語句,繼續時行循環體的下一次迭代。
例:
int main()
{
int count;
int con;
for(count=1; count<10; count++)
{
if(count==5)
break;
cout<<count<<" "; //1 2 3 4
}
cout<<"/nbreak out of loop at count="<<count<<endl;
for(con=1; con<10; con++)
{
if(con==5)
continue;
cout<<con<<" "; //1 2 3 4 6 7 8 9
}
cout<<"/nuse continue to skip printing 5"<<endl;
return 0;
}
break語句在while,for, do...while或者switch語句中執行時,立刻使程序控制退出這些語句。程序繼續勢利這些語句之後的第一條語句。break的常見用法是要麼提前離開循環,要麼用於跳過switch語句的剩餘部分。
continue:
continue在while, for, do...while語句執行中,使程序跳過循環體內剩下的語句,繼續時行循環體的下一次迭代。
例:
int main()
{
int count;
int con;
for(count=1; count<10; count++)
{
if(count==5)
break;
cout<<count<<" "; //1 2 3 4
}
cout<<"/nbreak out of loop at count="<<count<<endl;
for(con=1; con<10; con++)
{
if(con==5)
continue;
cout<<con<<" "; //1 2 3 4 6 7 8 9
}
cout<<"/nuse continue to skip printing 5"<<endl;
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.