如何在C / C ++中使用continue语句

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;
    else
        printf("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 = 10
    do {
        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)



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值