C 中的条件运算符

发布于 2024-08-27 18:11:55 字数 46 浏览 8 评论 0原文

每个 if...then...else 语句都可以仅使用 转换为等效语句吗?:

can every if...then...else statement be converted into an equivalent statement using only ?:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

儭儭莪哋寶赑 2024-09-03 18:11:55

代码:

if ( flag ) {
   exit(1);
}
else {
   return 0;
}

无法转换为:

flag ? exit(1) : return 0;

更好的示例 - 这将在循环内:

if ( flag ) {
   continue;
}
else {
   break;
}

无法转换为:

flag ? continue : break;

The code:

if ( flag ) {
   exit(1);
}
else {
   return 0;
}

cannot be converted into:

flag ? exit(1) : return 0;

A better example - this would be inside a loop:

if ( flag ) {
   continue;
}
else {
   break;
}

cannot be converted to:

flag ? continue : break;
囍孤女 2024-09-03 18:11:55

虽然我能想到的三元运算符的任何用途都可以实现为 if/else,但反之则不然;至少在不诉诸不正当且毫无意义的“技巧”的情况下,这些“技巧”在性能、可读性或可维护性方面不会带来任何好处。

if/else 的语法是:

if( <boolean expression> )
    <statment>|<statment block>
else
    <statment>|<statment block>

而 ?: 三元运算符的语法是:

<boolean expression> ? <expression> : <expression> ;

这里重要的是 是不同的句法元素。

形式的非常有限的用法:

if( b ) x = y else x = z ;

可以实现为:

x = b ? y : x ;

但这里的限制是在 true 和 false 子句中分配相同的变量(因此 y 和 z 至少都可以转换为 x 的类型) 。因此可以说任何条件赋值都可以用三元运算符来实现(毕竟这是它的主要目的)。

现在,由于函数调用是有效的表达式,因此您可以将 true 和 false 子句包装在单独的函数中,但这样做只是为了证明一点有点不正当:

if( b )
    true_stuff() ;
else
    false_stuff() ;

相当于:

b ? true_stuff() : false_stuff() ;

并且这些函数可以包含任何代码。

因此,要将更通用的 if/else 情况转换为 ?: 操作,必须首先将 true/false 语句块包装在单独的函数中。然而,即便如此,Neil Butterworth 的示例也将击败这种方法,因为 breakContinuereturn 的行为会影响超出 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:

if( <boolean expression> )
    <statment>|<statment block>
else
    <statment>|<statment block>

whereas the syntax of the ?: ternary operator is:

<boolean expression> ? <expression> : <expression> ;

The important thing here being that an <expression> and a <statement> are different syntactic elements.

The very limited usage of the form:

if( b ) x = y else x = z ;

can be implemented as:

x = b ? y : x ;

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:

if( b )
    true_stuff() ;
else
    false_stuff() ;

is equivalent to:

b ? true_stuff() : false_stuff() ;

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, and return 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 a goto in the if/else would also defeat this approach. .

I think in the end, even if you could, why would you want to?

半步萧音过轻尘 2024-09-03 18:11:55

不可以。

条件表达式的两个“分支”的计算结果必须为相同类型,并且该类型不得为 void

例如,您可以这样做:

x > 0 ? printf("Positive!\n") : 0;

因为 printf 返回 int。 (不过,我只会在一轮代码高尔夫中使用它;事实上, 我刚刚做了。)

但是你不能这样做:

x > 0 ? exit() : 0;

因为 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:

x > 0 ? printf("Positive!\n") : 0;

because printf return int. (I would only use this in a round of code golf, though; in fact, I just did.)

But you cannot do this:

x > 0 ? exit() : 0;

because exit returns void (or, actually, doesn't return at all).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文