[英]IF-ELSE statement shortcut in C
C has the following syntax for a shorthand IF-ELSE statement 对于速记IF-ELSE语句,C具有以下语法
(integer == 5) ? (TRUE) : (FALSE);
I often find myself requiring only one portion (TRUE or FALSE) of the statement and use this 我经常发现自己只需要声明的一部分(TRUE或FALSE)并使用它
(integer == 5) ? (TRUE) : (0);
I was just wondering if there was a way to not include the ELSE portion of the statement using this shorthand notation? 我只是想知道是否有办法不使用这种速记符号包含声明的ELSE部分?
The operator ?:
must return a value. 运算符
?:
必须返回一个值。 If you didn't have the "else" part, what would it return when the boolean expression is false? 如果你没有“else”部分,当布尔表达式为false时它会返回什么? A sensible default in some other languages may be null, but probably not for C. If you just need to do the "if" and you don't need it to return a value, then typing
if
is a lot easier. 其他一些语言中的合理默认值可能为空,但可能不适用于C.如果您只需要执行“if”而不需要它返回值,则输入
if
会更容易。
Question is whether we can somehow write the following expression without both then and else parts 问题是我们是否可以在没有then和else部分的情况下以某种方式编写以下表达式
(integer == 5) ? (THENEXPR) : (ELSEEXPR);
If you only need the then part you can use &&: 如果你只需要那个部分,你可以使用&&:
(integer == 5) && (THENEXPR)
If you only need the else part use ||: 如果你只需要使用else部分,请使用||:
(integer == 5) || (ELSEEXPR)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.