In this article, we’ll take a look at using the continue statement in C/C++.

在本文中,我们将研究在C / C ++中使用continue语句。

This is one of the easiest and the most basic keywords in C/C++, which gives programmers control over loops.

这是C / C ++中最简单,最基础的关键字之一,它使程序员可以控制循环。

Let’s take a quick look, using some examples!

让我们使用一些示例快速浏览一下!



在C / C ++中使用Continue语句 (Using the continue statement in C/C++)

The continue statement is exactly as the name suggests. Since we use this in loops, it will skip over the remaining body of the current loop, and continue to the next iteration.

继续声明与名称完全相同。 由于我们在循环中使用此方法,因此它将跳过当前循环的其余主体,并继续进行下一个迭代。

We can use this inside any loops like for, while, or do-while loops.

我们可以在forwhiledo-while循环之类的任何循环中使用它。

Let’s start with while loops now.

让我们从while循环开始。



将while语句与while循环一起使用 (Using the continue statement with a while loop)

We’ll take a simple example to illustrate this concept.

我们将举一个简单的例子来说明这个概念。


while (condition) {if (something)continue;elseprintf("Hi\n");
}

In this case, if something was True, we will continue onto the next iteration. So, in this case, Hi will not be printed, as the loop directly moves to the while(condition) check.

在这种情况下,如果something结果为True,我们将continue进行下一个迭代。 因此,在这种情况下, Hi将不会被打印,因为循环将直接移至while(condition)检查。

Take the below program, in C:

请在C中使用以下程序:


#include <stdio.h>int main() {int i = 1;while (i <= 5) {if (i == 3) {i++;continue;}printf("i = %d\n", i);i++;}return 0;
}

Output

输出量


1
2
4
5

In this case, we start from i=1 and keep iterating through the while loop. If i = 3, we increment i and continue to the next iteration.

在这种情况下,我们从i=1开始,并不断迭代while循环。 如果i = 3,我们将i递增并继续进行下一个迭代。

So, when i=3, it will not be printed, since we skip the body and proceed to the next iteration!

因此,当i = 3时,将不会打印它,因为我们跳过了主体并进行下一个迭代!

Note that we carefully placed an i++ increment before the continue, as otherwise, we will end up in an infinite loop, as the value of i won’t change when using continue normally!

请注意,我们将i++增量小心地放在了continue之前,否则,我们将陷入无限循环,因为当正常使用continue时, i的值不会改变!



使用继续和for循环 (Using continue along with a for loop)

We can write the same program, using a for loop too!

我们也可以使用for循环编写相同的程序!


#include <stdio.h>int main() {for (int i=1; i<=5; i++) {if (i == 3) {i++;continue;}printf("i = %d\n", i);}return 0;
}

We get the same output as before.

我们得到与以前相同的输出。



使用do-while循环继续 (Using continue with a do-while loop)

We can also use this on a do-while loop, since this is also an iterative loop.

我们也可以在do-while循环中使用它,因为这也是一个迭代循环。


#include <stdio.h>int main() {int i=10; // Set i = 10do {if (i > 5) { // Initially, we will go to this statement, as it is a do-while loop!i = 0;continue;}printf("i = %d\n", i);} while (i <= 5)return 0;
}

In this case, since we have a do-while loop, we go to the body of the loop before we do our condition check. So, when i=10, we reassign it to 0 and continue. So, the output will simply be integers from 0 to 5!

在这种情况下,由于我们有一个do-while循环,因此在执行条件检查之前,我们先进入循环的主体。 因此,当i=10 ,我们将其重新分配为0并继续。 因此,输出将只是0到5的整数!

Output

输出量


0
1
2
3
4
5


结论 (Conclusion)

In this article, we learned how we could use the continue statement in C/C++ to have control over loop statements!

在本文中,我们学习了如何在C / C ++中使用continue语句来控制循环语句!

If you want similar articles on C, do go through our C programming tutorials!

如果您想要有关C的类似文章,请阅读我们的C编程教程 !



参考资料 (References)

  • cppreference.com page on Continue有关继续的cppreference.com页面


翻译自: https://www.journaldev.com/38218/continue-statement-in-c-plus-plus

如何在C / C ++中使用continue语句相关推荐

  1. python循环语句continue_在Python中嵌套for循环中使用Continue语句

    嗨,我有一个从两个文件中读取数据的函数.我想要的是外部循环开始读取第一个文件,如果第一个文件中的lum大于定义值(LC),那么循环将跳到下一个迭代.如果没有,代码将移动到 内部循环,读取hlist并在 ...

  2. 如何在sql存储过程中打log_SQL语句优化

    如何定位并优化慢查询 SQL?大致思路如下: 根据慢查询日志定位慢查询 SQL: 使用 explain 等工具分析 SQL: 修改 SQL 或者尽量让 SQL 走索引. 获取有性能问题的 SQL 的 ...

  3. js中的continue语句

    continue关键字用于立即跳出本次循环,继续下一次循环 (本次循环体中continue之后的代码就会少执行一次) 例如:吃5个包子,第三个有虫子,就扔掉第3个,继续吃第4个第5个包子 <sc ...

  4. JavaScript Continue语句

    在JavaScript中,你可以使用continue语句重新启动循环的新迭代.该语句可以在while循环,for循环或for-in循环中使用 continue语句终止当前循环或标记循环的当前迭代中的语 ...

  5. continue语句只用于循环语句中_循环里continue,break,return的作用,你知道吗?

    循环里continue,break,return的作用,你知道吗?​mp.weixin.qq.com 前言 循环里Continue,Break,Return经常会用到,也是很容易出错的一个坑,今天特地 ...

  6. continue语句可以用在switch语句和3种循环语句中_必须知道的C语言知识细节:break、continue语句区别...

    break语句.continue语句都是C语言标准规定的跳转类语句,能够实现程序无条件转向另一处执行. break和continue中在循环体中经常出现,因此必须掌握其区别,避免出错. 先复习下两种语 ...

  7. Python中break语句和continue语句的用法

    在Python中,break语句和continue语句一般用于循环语句中. 1 break语句 1.1 break语句在单循环中的使用 在单循环中的break语句,其作用是结束当前循环,代码如图1所示 ...

  8. Java教程:Java continue语句详解

    Java教程有时强迫一个循环提早反复是有用的,也就是,你可能想要继续运行循环,但是要忽略这次重复剩余的循环体的语句,所以 Java 提供了 continue 语句.continue 语句是 break ...

  9. Swift的控制转移语句--continue语句

    2019独角兽企业重金招聘Python工程师标准>>> 控制转移语句能够改变程序的执行顺序,可以实现程序的跳转.Swift有3种控制转移语句: continue.break.fall ...

最新文章

  1. C++11中值得关注的几大变化 .
  2. python软件安装-学python安装的软件总结
  3. 【Groovy】MOP 元对象协议与元编程 ( 方法合成 | 动态注入方法 )
  4. 在c语言中log函数的作用,C++_在C语言中使用对数函数的方法,C语言log()函数:返回以e为底的 - phpStudy...
  5. linux查看睡眠进程,关于 Linux 进程的睡眠和唤醒 ,来看这篇就够了~
  6. html5和css3打造一款创意404页面
  7. 永辉生活APP卖茅台只收款不发货,永辉超市回应...
  8. 中文拼写纠错_58搜索拼写纠错
  9. [DELPHI]数据类型
  10. Java基础学习总结(70)——开发Java项目常用的工具汇总
  11. java中国象棋规则_Java实践(十二)——中国象棋
  12. C# 连接本地数据库
  13. 显卡刷bios改型号_显卡BIOS刷新方法(详细)
  14. 51单片机原理以及接口技术(四)--80C51的程序设计
  15. Wireshark 捕捉本地数据 --WinPcap切换NPcap
  16. 端口汇聚实现多端口带宽叠加
  17. 怎样注册完申请个人电子邮箱?2022邮箱号码大全速看
  18. 对不起,今年我真的不敢去拜年了。。。
  19. gitlab ip变更runner拉取代码失败、shell启动springboot项目启动起来。
  20. VC++操作Excel生成饼状图!

热门文章

  1. Cut Curve randomly
  2. 对报表模型项应用安全筛选器
  3. [转载] python 时间sleep() 的方法
  4. 端口截听实现端口隐藏 嗅探与攻击
  5. CSRF(跨站请求伪造)攻击 --
  6. CentOS7更改时区两步解决
  7. 通俗易懂的理解 Redux(知乎)
  8. 【转载】产品经理如何行之有效的提高执行力
  9. 本示例主要展示如何在XtraGrid网格控件(包含在DevExpress WinForms套包中)的主视图中指定HyperLinkEdit控件作为列编辑器...
  10. Android 递归删除文件和文件夹