continue
語句
來自 cppreference.com
導致跳過封閉的 for、range-for、while 或 do-while 迴圈體的剩餘部分。
當使用條件語句忽略迴圈的剩餘部分很不方便時使用。
目錄 |
[編輯] 語法
attr (可選) continue ; |
|||||||||
[編輯] 解釋
continue
語句導致跳轉,如同透過 goto 跳轉到迴圈體的末尾(它只能出現在 for、range-for、while 和 do-while 迴圈的迴圈體中)。
更精確地說,
對於 while 迴圈,它等同於
while (/* ... */) { // ... continue; // acts as goto contin; // ... contin:; }
對於 do-while 迴圈,它等同於
do { // ... continue; // acts as goto contin; // ... contin:; } while (/* ... */);
for (/* ... */) { // ... continue; // acts as goto contin; // ... contin:; }
[編輯] 關鍵詞
[編輯] 示例
執行此程式碼
#include <iostream> int main() { for (int i = 0; i < 10; ++i) { if (i != 5) continue; std::cout << i << ' '; // this statement is skipped each time i != 5 } std::cout << '\n'; for (int j = 0; 2 != j; ++j) for (int k = 0; k < 5; ++k) // only this loop is affected by continue { if (k == 3) continue; // this statement is skipped each time k == 3: std::cout << '(' << j << ',' << k << ") "; } std::cout << '\n'; }
輸出
5 (0,0) (0,1) (0,2) (0,4) (1,0) (1,1) (1,2) (1,4)
[編輯] 參閱
C 文件 關於 continue
|