🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# C++ 中的`continue`语句 > 原文: [https://beginnersbook.com/2017/08/cpp-continue-statement/](https://beginnersbook.com/2017/08/cpp-continue-statement/) 在循环内使用`continue`语句。每当在循环内遇到`continue`语句时,控制流直接跳转到循环的开头以进行下一次迭代,跳过循环体内当前迭代的语句的执行。 #### `continue`语句的语法 ```cpp continue; ``` ## 示例:`for`循环中的`continue`语句 正如你可以看到输出缺少值 3,但循环迭代`num`值 0 到 6。这是因为我们在循环中设置了一个条件,这种情况下当`num`值等于 3 时遇到语句。因此,对于此迭代,循环跳过`cout`语句并开始下一次循环迭代。 ```cpp #include <iostream> using namespace std; int main(){ for (int num=0; num<=6; num++) { /* This means that when the value of * num is equal to 3 this continue statement * would be encountered, which would make the * control to jump to the beginning of loop for * next iteration, skipping the current iteration */ if (num==3) { continue; } cout<<num<<" "; } return 0; } ``` **输出:** ```cpp 0 1 2 4 5 6 ``` ## `continue`语句的流程图 ![C++ continue statement](https://img.kancloud.cn/5f/f0/5ff0abe92e9f78b98451df7d3022d000_400x400.jpg) ## 示例:在`while`循环中使用`continue` ```cpp #include <iostream> using namespace std; int main(){ int j=6; while (j >=0) { if (j==4) { j--; continue; } cout<<"Value of j: "<<j<<endl; j--; } return 0; } ``` **输出:** ```cpp Value of j: 6 Value of j: 5 Value of j: 3 Value of j: 2 Value of j: 1 Value of j: 0 ``` ## `do-while`循环中`continue`的示例 ```cpp #include <iostream> using namespace std; int main(){ int j=4; do { if (j==7) { j++; continue; } cout<<"j is: "<<j<<endl; j++; }while(j<10); return 0; } ``` **输出:** ```cpp j is: 4 j is: 5 j is: 6 j is: 8 j is: 9 ```