名稱空間
變體
操作

continue 語句

來自 cppreference.com
< c‎ | 語言

導致跳過其外圍 forwhile do-while 迴圈體的剩餘部分。

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

目錄

[編輯] 語法

屬性說明序列(可選) continue ;
屬性說明序列 - (C23)可選的屬性列表,應用到 continue 語句

[編輯] 解釋

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

對於 while 迴圈,其行為是

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

對於 do-while 迴圈,其行為是

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

對於 for 迴圈,其行為是

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

[編輯] 關鍵詞

continue

[編輯] 示例

#include <stdio.h>
 
int main(void) 
{
    for (int i = 0; i < 10; i++) {
        if (i != 5) continue;
        printf("%d ", i);             // this statement is skipped each time i != 5
    }
 
    printf("\n");
 
    for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 5; k++) { // only this loop is affected by continue
            if (k == 3) continue;
            printf("%d%d ", j, k);    // this statement is skipped each time k == 3
        }
    }
}

輸出

5
00 01 02 04 10 11 12 14

[編輯] 引用

  • C17 標準 (ISO/IEC 9899:2018)
  • 6.8.6.2 The continue statement (p: 111)
  • C11 標準 (ISO/IEC 9899:2011)
  • 6.8.6.2 The continue statement (p: 153)
  • C99 標準 (ISO/IEC 9899:1999)
  • 6.8.6.2 The continue statement (p: 138)
  • C89/C90 標準 (ISO/IEC 9899:1990)
  • 3.6.6.2 The continue statement

[編輯] 參閱

C++ 文件中關於continue 語句的內容