continue 文
From cppreference.com
自身を囲む for、while、または do-while ループ本体の残りの部分をスキップさせます。
条件文を使ってループの残りの部分を無視するのが厄介な場合に使用されます。
目次 |
[編集] 構文
attr-spec-seq(optional) continue ; |
|||||||||
| attr-spec-seq | - | (C23以降)continue 文に適用される属性の任意のリスト |
[編集] 解説
continue 文は、あたかも goto のように、ループ本体の末尾へのジャンプを引き起こします(for、while、do-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:; }
[編集] キーワード
[編集] 例
このコードを実行
#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
[編集] 関連項目
continue 文の C++ ドキュメント |