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
.
现在,这与其他循环语句(如for
和while
)略有不同。
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)
- Microsoft Documentation on C/C++ page on break statement C / C ++页面上的Microsoft文档中的 break语句
翻译自: https://www.journaldev.com/38240/break-statement-in-c-plus-plus
c++ break语句