原文: https://beginnersbook.com/2017/08/cpp-continue-statement/
在循环内使用continue语句。每当在循环内遇到continue语句时,控制流直接跳转到循环的开头以进行下一次迭代,跳过循环体内当前迭代的语句的执行。
continue语句的语法
continue;
示例:for循环中的continue语句
正如你可以看到输出缺少值 3,但循环迭代num值 0 到 6。这是因为我们在循环中设置了一个条件,这种情况下当num值等于 3 时遇到语句。因此,对于此迭代,循环跳过cout语句并开始下一次循环迭代。
#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;}
输出:
0 1 2 4 5 6
continue语句的流程图

示例:在while循环中使用continue
#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;}
输出:
Value of j: 6Value of j: 5Value of j: 3Value of j: 2Value of j: 1Value of j: 0
do-while循环中continue的示例
#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;}
输出:
j is: 4j is: 5j is: 6j is: 8j is: 9
