名稱空間
變體
操作

continue 語句

來自 cppreference.com
< cpp‎ | 語言
 
 
C++ 語言
 
 

導致跳過封閉的 forrange-forwhiledo-while 迴圈體的剩餘部分。

當使用條件語句忽略迴圈的剩餘部分很不方便時使用。

目錄

[編輯] 語法

attr (可選) continue ;

[編輯] 解釋

continue 語句導致跳轉,如同透過 goto 跳轉到迴圈體的末尾(它只能出現在 forrange-forwhiledo-while 迴圈的迴圈體中)。

更精確地說,

對於 while 迴圈,它等同於

while (/* ... */)
{
   // ...
   continue; // acts as goto contin;
   // ...
   contin:;
}

對於 do-while 迴圈,它等同於

do
{
    // ...
    continue; // acts as goto contin;
    // ...
    contin:;
} while (/* ... */);

對於 forrange-for 迴圈,它等同於

for (/* ... */)
{
    // ...
    continue; // acts as goto contin;
    // ...
    contin:;
}

[編輯] 關鍵詞

continue

[編輯] 示例

#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