C 中的条件运算符
每个 if...then...else 语句都可以仅使用 转换为等效语句吗?:
can every if...then...else statement be converted into an equivalent statement using only ?:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码:
无法转换为:
更好的示例 - 这将在循环内:
无法转换为:
The code:
cannot be converted into:
A better example - this would be inside a loop:
cannot be converted to:
虽然我能想到的三元运算符的任何用途都可以实现为 if/else,但反之则不然;至少在不诉诸不正当且毫无意义的“技巧”的情况下,这些“技巧”在性能、可读性或可维护性方面不会带来任何好处。
if/else 的语法是:
而 ?: 三元运算符的语法是:
这里重要的是
和
是不同的句法元素。形式的非常有限的用法:
可以实现为:
但这里的限制是在 true 和 false 子句中分配相同的变量(因此 y 和 z 至少都可以转换为 x 的类型) 。因此可以说任何条件赋值都可以用三元运算符来实现(毕竟这是它的主要目的)。
现在,由于函数调用是有效的表达式,因此您可以将 true 和 false 子句包装在单独的函数中,但这样做只是为了证明一点有点不正当:
相当于:
并且这些函数可以包含任何代码。
因此,要将更通用的 if/else 情况转换为 ?: 操作,必须首先将 true/false 语句块包装在单独的函数中。然而,即便如此,Neil Butterworth 的示例也将击败这种方法,因为
break
、Continue
和return
的行为会影响超出 if 范围的控制流/else 构造(尽管也许这些也是您想要避免的代码示例!)。 if/else 中goto
的存在也会破坏这种方法。 。我想最后,即使你可以,你为什么要这么做呢?
While any use I can think of for the ternary operator can be implemented as an if/else, the converse is not true; at least not without resorting to perverse and pointless 'tricks' that yield no benefit in terms of performance, readability, or maintainability.
The syntax of if/else is:
whereas the syntax of the ?: ternary operator is:
The important thing here being that an
<expression>
and a<statement>
are different syntactic elements.The very limited usage of the form:
can be implemented as:
but here the restriction is that the same variable is being assigned in both the true and false clauses, (and therefore y and z are both at least convertible to the type of x). So it may be said that any conditional assignment can be implemented with the ternary operator (after all that is its primary purpose).
Now since a function call is a valid expression, you could wrap the true and false clauses in separate functions, but to do that simply to prove a point is somewhat perverse:
is equivalent to:
and those functions can contain any code at all.
So to convert the more general if/else case to a ?: operation, the true/false statement blocks must first be wrapped in separate functions. However even then Neil Butterworth's examples will defeat this approach since the behaviour of
break
,continue
, andreturn
affect control flow beyond the confines of the if/else construct, (although perhaps those are also examples of code you want to avoid!). The presence of agoto
in the if/else would also defeat this approach. .I think in the end, even if you could, why would you want to?
不可以。
条件表达式的两个“分支”的计算结果必须为相同类型,并且该类型不得为
void
。例如,您可以这样做:
因为
printf
返回int
。 (不过,我只会在一轮代码高尔夫中使用它;事实上, 我刚刚做了。)但是你不能这样做:
因为
exit
返回void
(或者,实际上,根本不返回)。No.
Both "branches" of the conditional expression must evaluate to the same type, and that type must not be
void
.For example, you could do this:
because
printf
returnint
. (I would only use this in a round of code golf, though; in fact, I just did.)But you cannot do this:
because
exit
returnsvoid
(or, actually, doesn't return at all).