C语言 具有多个条件的 if 的执行顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2456086/
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
Order of execution for an if with multiple conditionals
提问by mrks
In an if statement with multiple conditionals, is the second conditional executed if the outcome of the first is clear?
在具有多个条件的 if 语句中,如果第一个条件的结果明确,是否执行第二个条件?
example:
例子:
if(i>0 && array[i]==0){
}
If I swap the conditionals a segfault may occur for negative values of i, but this way no segfault occurs. Can I be sure that this always works or do have have to use nested if statements?
如果我交换条件,则 i 的负值可能会发生段错误,但这样就不会发生段错误。我可以确定这总是有效还是必须使用嵌套的 if 语句?
回答by Uri
This type of evaluation is called short-circuiting. Once the result is 100% clear, it does not continue evaluating.
这种类型的评估称为短路。一旦结果 100% 清晰,就不会继续评估。
This is actually a common programming technique. For example, in C++ you will often see something like:
这实际上是一种常见的编程技术。例如,在 C++ 中,您经常会看到如下内容:
if (pX!=null && pX->predicate()) { bla bla bla }
If you changed the order of the conditions, you could be invoking a method on a null pointer and crashing. A similar example in C would use the field of a struct when you have a pointer to that struct.
如果您更改了条件的顺序,您可能会在空指针上调用方法并崩溃。当您有一个指向该结构的指针时,C 中的一个类似示例将使用该结构的字段。
You could do something similar with or:
你可以用 or 做类似的事情:
if(px==null || pX->isEmpty()} { bla bla bla }
This is also one of the reasons that it is generally a good idea to avoid side effects in an if condition.
这也是在 if 条件下避免副作用通常是个好主意的原因之一。
For example suppose you have:
例如,假设您有:
if(x==4 && (++y>7) && z==9)
If xis 4, then ywill be incremented regardless of the value of zor y, but if xis not 4, it will not be incremented at all.
如果x是4,则y无论z或的值如何都会增加y,但如果x不是4,则根本不会增加。
回答by John Bode
The operators &&and ||guarantee that the left-hand side expression will be fully evaluated (and all side effects applied) before the right-hand side is evaluated. In other words, the operators introduce a sequence point.
运算符&&and||保证在评估右侧表达式之前将完全评估左侧表达式(并应用所有副作用)。换句话说,运算符引入了一个序列点。
Additionally, if the value of the expression can be determined from the lhs, the rhs is not evaluated. In other words, if you have an expression like x && y, and x evaluates to 0 (false), then the value of the expression is false regardless of y, so y is not evaluated.
此外,如果表达式的值可以从 lhs 确定,则不计算 rhs。换句话说,如果您有一个类似 的表达式x && y,并且 x 的计算结果为 0(假),那么无论 y 是什么,表达式的值都是假的,因此不会计算 y。
This means that expressions like x++ && x++are well-defined, since &&introduces a sequence point.
这意味着像这样的表达式x++ && x++是明确定义的,因为&&引入了一个序列点。
回答by Shiplu Mokaddim
From draft 3485 (n3485.pdf) Its clearly stated that
从草案 3485 (n3485.pdf) 中明确指出
5.14 Logical AND operator [expr.log.and]
logical-and-expression: inclusive-or-expression logical-and-expression && inclusive-or-expression
- The && operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.
- The result is a bool. If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression.
5.14 逻辑与运算符 [expr.log.and]
logical-and-expression: inclusive-or-expression logical-and-expression && inclusive-or-expression
- && 运算符从左到右分组。操作数都根据上下文转换为 bool(第 4 条)。如果两个操作数都为真,则结果为真,否则为假。与 & 不同,&& 保证从左到右的计算:如果第一个操作数为假,则不计算第二个操作数。
- 结果是一个布尔值。如果计算第二个表达式,则与第一个表达式关联的每个值计算和副作用在与第二个表达式关联的每个值计算和副作用之前排序。

