c++ break语句_如何在C / C ++中使用break语句

c++ break语句

In this article, we’ll take a look at how we can use the break statement in C/C++.

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

Similar to the continue statement, we can use the break statement to manipulate loop control.

类似于continue语句 ,我们可以使用break语句来操纵循环控制。

Let’s take a look at this, using some examples!

让我们用一些例子来看看!



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

While we can use the continue statement to jump to the next iteration of the loop, the break statement breaks out of the loop.

虽然我们可以使用continue语句跳转到循环的下一个迭代,但break语句会跳出循环。

This jumps to the next instruction proceeding the loop body while maintaining the state of all non-local variables of the loop.

这跳转到下一条指令,继续循环主体,同时保持循环的所有非局部变量的状态。

To see how this works out, let’s look at an example, using while loops.

要查看其效果,让我们来看一个使用while循环的示例。

在while循环中使用break语句 (Using break statement inside a while loop)

We can use break to directly exit a while loop.

我们可以使用break直接退出while循环。


while(cond) {
    if (something)
        break;
    // Main body
}

Consider the below while loop, which iterates until i > 10.

考虑下面的while循环,该循环迭代直到i> 10。


#include <stdio.h>

int main() {
    int i = 0;
    while (i <= 10) {
        if (i == 5)
            break;
        printf("i = %d\n", i);
    }
    printf("Outside the Loop\n");
    return 0;
}

If you see what I have done, this program will keep printing the value of i until 10. But, if i == 5, it will break out from the loop directly.

如果看到我做了什么,该程序将一直打印i的值直到10。但是,如果i == 5 ,它将直接从循环中退出。

The output describes what is happening:

输出描述了正在发生的事情:


i = 0
i = 1
i = 2
i = 3
i = 4
Outside the Loop

Hopefully, that clears things up regarding break inside a while loop.

希望这可以清除有关while循环内break的信息。

Similar to the while loop, we can also break from the loop control of a do-while loop in an almost-identical fashion.

while循环类似,我们也可以以几乎完全相同的方式摆脱do-while循环的循环控制。

Next, we’ll look at a similar example, using a for loop.

接下来,我们将使用for循环查看类似的示例。

在for循环中使用break语句 (Using the break statement inside a for loop)

We can replicate the above program using a for loop as well!

我们也可以使用for循环复制上面的程序!


#include <stdio.h>

int main() {
    for (int i=0; i<10; i++) {
        if (i == 5)
            break;
        printf("i = %d\n", i);
    }
    printf("Outside the Loop\n");
    return 0;
}

This will have the same output as before since we immediately break away when i == 5.

这将具有与以前相同的输出,因为当i == 5时我们会立即脱离。

在switch语句中使用break语句 (Using the break statement inside a switch statement)

Now, this is slightly different from other loop statements like for and while.

现在,这与其他循环语句(如forwhile )略有不同。

A switch statement is a statement which tries to map a target value to a case value.

switch语句是试图将目标值映射到case值的语句。

If they match, the relevant block is executed. Otherwise, it keeps moving down, until the block is over, or a default block is encountered.

如果它们匹配,则执行相关的块。 否则,它将继续向下移动,直到该块结束或遇到default块为止。

If we write a switch statement like the below snippet, we may unknowingly introduce bugs to our program.

如果我们像下面的代码片段那样编写switch语句,可能会在不知不觉中向程序引入错误。


#include <stdio.h>

int main() {
    for (int i=0; i<=2; i++) {
        switch(i) {
            case 2:
            printf("i is equal to 2\n");
            default:
            printf("i is not equal to 2\n");
        }
    }
    return 0;
}

Output

输出量


i is not equal to 2
i is not equal to 2
i is equal to 2
i is not equal to 2

Why are we getting the last two lines, even though i lies between 0 to 2? The answer is because the default case is getting executed even if i == 2. The switch statement doesn’t stop if one case gets matched.

即使我介于0到2之间,为什么还要得到最后两行? 答案是因为即使i == 2也会执行default情况。 如果匹配一种情况, switch语句不会停止。

To avoid this, we must explicitly tell the program to break away from the switch statement if any case is matched.

为了避免这种情况,我们必须明确地告诉程序break ,如果任何情况下匹配从switch语句了。

So, the common practice is to add the break statement at the end of every case statement!

因此,通常的做法是在每个case语句的末尾添加break语句!


#include <stdio.h>

int main() {
    for (int i=0; i<=2; i++) {
        switch(i) {
            case 2:
            printf("i is equal to 2\n");
            break; // Add break condition
            default:
            printf("i is not equal to 2\n");
            break; // Add break condition
        }
    }
    return 0;
}

Here, I don’t need to use break after default, but it is a good practice to do so.

在这里,我不需要使用default之后的break ,但是这样做是一个好习惯。

Output

输出量


i is not equal to 2
i is not equal to 2
i is equal to 2

Now we get the correct output, and our problem is solved!

现在我们得到正确的输出,并且我们的问题得到解决!

Hopefully, you now understand the usefulness of the break statement in C/C++!

希望您现在了解C / C ++中break语句的用处!



结论 (Conclusion)

In this article, we learned about using the break statement in C/C++ to break out of loops and case statement blocks.

在本文中,我们学习了如何在C / C ++中使用break语句来突破循环和case语句块。



参考资料 (References)



翻译自: https://www.journaldev.com/38240/break-statement-in-c-plus-plus

c++ break语句

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值