简写c ++ if else语句 [英] shorthand c++ if else statement
问题描述
所以我只是好奇,如果有一个简短的声明到这:
So I'm just curious if there is a short hand statement to this:
if(number < 0 )
bigInt.sign = 0;
else
bigInt.sign = 1;
我看到所有这些简短的陈述,如果一个< b等等。
I see all these short hand statements for if a < b and such.
我不知道如何正确地做,并希望一些输入。
I'm not sure on how to do it properly and would like some input on this.
谢谢!
我实际上只是在你们回答之前才想出来。
I actually just figured it out right before you guys had answered.
m使用 bigInt.sign =(number< 0)? 1:0
推荐答案
是:
bigInt.sign = !(number < 0);
!
运算符总是计算为 true
或 false
。转换为 int
时,它们分别变为 1
和 0
。
The !
operator always evaluates to true
or false
. When converted to int
, these become 1
and 0
respectively.
当然,这相当于:
bigInt.sign = (number >= 0);
这里括号是多余的,所有的比较和关系运算符都计算 true
或 false
。
Here the parentheses are redundant but I add them for clarity. All of the comparison and relational operator evaluate to true
or false
.
这篇关于简写c ++ if else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!