C ++示例中的Continue语句| C ++继续声明程序

C ++示例中的Continue语句| C ++ Continue Statement Program是今天的主题。 Continue与break语句并行工作。唯一的区别是break语句完全终止了循环,而continue语句仅跳过了当前迭代并转到了下一个迭代。 Continue语句的主要目的是针对当前迭代跳过循环体内的语句执行。

内容概述

  • 1 C ++中的Continue语句
    • 1.1 Continue语句的语法
  • 2继续声明流程图
  • 3继续声明的示例
    • 3.1在for循环中继续执行语句
  • 4在While循环中使用continue语句
  • 5在do-While循环中使用Continue
    • 5.1 C ++带有内部循环的Continue语句
  • 6篇推荐文章

C ++中的Continue语句

顾名思义,continue语句强制循环继续执行或执行下一个迭代。

当在循环中执行continue语句时,continue语句之后的循环内的代码将被跳过,并且循环的下一个迭代将开始。

例如,假设我们要打印从1到50的数字,并且我们不想打印45,那么当循环迭代达到use 45时,我们可以使用continue语句,该语句将跳过该迭代的执行并转到开头下一次迭代。

Continue语句的语法

continue; 

请参见以下代码段。

for (int i = 1; i <= 50; i++) {   cout << i << endl;   if (i == 45)   {     continue;   } } 

它将打印从1到50的所有数字,除了45。

继续声明流程图

继续声明流程图

继续声明的示例

在for循环中继续执行语句

请参阅下面的代码,其中我们将for语句用于for循环。

#include  using namespace std;  int main() {   for (int i = 0; i <= 10; i++)   {     if (i == 5)     {       continue;     }     cout << "i=" << i << endl;   }   cout << "Missed it?n Check 5.n Not there?n We used continue over there to skip it" << endl; }

查看输出。

C ++示例中的Continue语句

Q2-编写程序以打印10的表,但是当它变成90时跳过该值,并打印其余的表。

#include  using namespace std;  int main() {   int k;   for (int i = 0; i <= 10; i++)   {     if (k == 80)     {       k = 0;       continue;     }     k = 0;     k = i * 10;     cout << "10 x " << i << "=" << k << endl;   }   cout << "I have skipped 10 x 9 = 90" << endl; }

查看输出。

C ++继续声明程序

在While循环中使用continue语句

请参见以下带有while循环的Continue语句程序。

#include  using namespace std;  int main() {   int e = 5;   while (e >= 0)   {     if (e == 4)     {       e--;       continue;     }     cout << "Value of e: " << e << endl;     e--;   }   return 0; }

请参阅以下输出。

Value of e: 5 Value of e: 3 Value of e: 2 Value of e: 1 Value of e: 0

在do-While循环中使用Continue

请参见以下带有do-while循环的continue语句程序。

#include  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: 4 j is: 5 j is: 6 j is: 8 j is: 9

C ++带有内部循环的Continue语句

仅当您在内部循环中使用continue语句时,C ++ Continue语句才会继续内部循环。请参阅以下代码。

#include  using namespace std;  int main() {   for (int i = 1; i <= 3; i++)   {     for (int j = 1; j <= 3; j++)     {       if (i == 2 && j == 2)       {         continue;       }       cout << i << " " << j << "n";     }   } }

查看输出。

1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3

最后,带有示例的Continue Statement Tutorial已经结束。

推荐的帖子

C ++切换范例

C ++示例教程中的If-else语句

C ++教程中的递归程序

C ++访问说明符

C ++中的异常处理

资讯来源:由0x资讯编译自APPDIVIDEND,版权归作者Ankit Lathiya所有,未经许可,不得转载
你可能还喜欢