当前位置: 首页 > 文档资料 > C++ 中文教程 >

continue statement

优质
小牛编辑
121浏览
2023-12-01

continue语句的工作方式与break语句类似。 但是,不是强制终止,而是继续强制执行循环的下一次迭代,跳过其间的任何代码。

对于for循环,continue会导致条件测试并增加循环的部分以执行。 对于whiledo...while循环,程序控制传递给条件测试。

语法 (Syntax)

C ++中continue语句的语法是 -

continue;

流程图 (Flow Diagram)

C ++继续声明

例子 (Example)

#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