C++ continue 語句



continue 語句的工作方式有點像 break 語句。但是,它不是強制終止,而是強制執行迴圈的下一個迭代,跳過中間的任何程式碼。

對於for迴圈,continue 會導致執行迴圈的條件測試和增量部分。對於whiledo...while迴圈,程式控制會傳遞到條件測試。

語法

C++ 中 continue 語句的語法如下:

continue;

流程圖

C++ continue statement

示例

#include <iostream>
using namespace std;
 
int main () {
   // Local variable declaration:
   int a = 10;

   // do loop execution
   do {
      if( a == 15) {
         // skip the iteration.
         a = a + 1;
         continue;
      }
      cout << "value of a: " << a << endl;
      a = a + 1;
   } 
   while( a < 20 );
 
   return 0;
}

編譯並執行上述程式碼後,將產生以下結果:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
廣告