如果声明与?和: [英] If statement with ? and :

查看:74
本文介绍了如果声明与?和:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说在C
$ b $中使用的I​​f语句b我不知道如何使用它,也找不到任何东西。
我需要使用它来缩短我的代码
,我们将不胜感激。

I heard about a kind of If statement which use ? and : in C
I dont know how to use it and I cant find anything about it. I need to use it in order to shorten my code any help would be appreciated.

推荐答案

?:三元运算符 C(也称为条件运算符)。您可以缩短代码,例如

?: is ternary operator in C (also called conditional operator). You can shorten your code like

if(condition)
    expr1;
else
    expr2;  

condition ? expr1 : expr2;   

查看其工作原理:


第一个操作数被求值;在它的评估与第二个或第三个操作数的
评估(无论哪个被评估)之间存在一个序列点。仅当第一个操作数比较不等于 0 时,才对第二个操作数
求值;仅当第一个操作数等于 0 时,才对第三个操作数求值;结果是第二个或第三个操作数
的值(以评估为准),

The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated),

这篇关于如果声明与?和:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆