C语言 `break` 是否仅适用于 `for`、`while`、`do-while`、`switch' 和 `if` 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2565659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does `break` work only for `for`, `while`, `do-while`, `switch' and for `if` statements?
提问by avi
Suppose, I have a ifstatement inside a forloop:
假设,我if在for循环中有一条语句:
for( ; ; )
{
if( )
{
printf(" inside if");
break;
}//if
printf("inside for");
}//for
Now, will the breakstatement cause the compiler to come out of the forloop or will it only come out of the body of ifonce the condition in the ifbecomes satisfied?
现在,该break语句会导致编译器退出for循环还是仅if在if满足条件时才退出循环?
回答by Windows programmer
The breakstatement breaks out of the nearest enclosing loopor switchstatement.
该break语句跳出最近的封闭循环或switch语句。
breakdoes not break out of an ifstatement, but the nearest loopor switchthat contains that ifstatement. The reason for not breaking out of an ifstatement is because it is commonly used to decide whether you want to break out of the loop.
break不脱离if语句,而是最近的loop或switch包含该if语句的。不跳出if语句的原因是因为它通常用于决定是否要跳出loop.
Interestingly, a telephone switch misbehaved because the company that invented C made exactly this bug. They wanted to break out of an ifstatement and they forgot that it would break out of the entire forstatement.
有趣的是,电话交换机行为不端,因为发明 C 的公司正是制造了这个错误。他们想打破一个if声明,他们忘记了它会打破整个for声明。
回答by Mark Byers
It will break out of the forloop. A breakstatement only has an effect on loops (do, for, while) and switchstatements (for breaking out of a case).
它将跳出for循环。一个break语句只对循环(一个效果do,for,while)和switch(为的突破语句case)。
From the C99 standard, section 6.8.6.3:
来自C99 标准,第 6.8.6.3 节:
Constraints
A break statement shall appear only in or as a switch body or loop body.
Semantics
A break statement terminates execution of the smallest enclosing switch or iteration statement.
约束
break 语句只能出现在或作为开关体或循环体。
语义
break 语句终止最小封闭 switch 或迭代语句的执行。
回答by Matti Virkkunen
breakwill not break out of an if clause, but the nearest loop or switch clause. Also, an if clause is not called a "loop" because it never repeats its contents.
break不会跳出 if 子句,而是跳出最近的循环或 switch 子句。此外,if 子句不称为“循环”,因为它从不重复其内容。
回答by The joker
Break statement takes effect only on loops and switch clause. In any case it will come out from the nearest enclosing loop. It's a good practice to use a conditional break i.e. break enclosed by some if statement. If you want to come out of all the loops or some loop you can use goto statement. For example:
Break 语句仅对循环和 switch 子句有效。在任何情况下,它都会从最近的封闭循环中出来。使用条件中断(即由某些 if 语句括起来的中断)是一种很好的做法。如果您想退出所有循环或某些循环,您可以使用 goto 语句。例如:
while(true) {
while (some_condition) {
if (some_condition)
goto label;
}
}
label: // do something here
回答by Gaurav Kumar
Break statement will not break any if or else statement. if you are using break statement it mainly come out from the nearest loop(for loop, while loop, do while loop and switch case)
Break 语句不会破坏任何 if 或 else 语句。如果您使用 break 语句,它主要来自最近的循环(for 循环、while 循环、do while 循环和 switch case)
回答by Cool girl
The break statement has no use in decison making statements. It is used only in loops, when you want to force termination from the loop and continue execution from the statement following the loop. There's no question of "will break statement cause the control to come out of the if statement if condition is true" , because irrespective of the break statement the control will anyway come out of the if statement if the condition is true and once its body is executed. The if statement is not a loop . it is either not exected at all or exectuted just once. So it absolutely makes no sense to put a break inside the body of if.
break 语句在决策语句中没有用处。它仅在循环中使用,当您想强制终止循环并从循环后面的语句继续执行时。不存在“如果条件为真,则 break 语句会导致控制从 if 语句中出来”的问题,因为无论 break 语句如何,如果条件为真,并且一旦其主体为执行。if 语句不是循环。它要么根本不执行,要么只执行一次。所以在 if 的主体内放置一个中断绝对没有意义。

