简写c ++ if else语句 [英] shorthand c++ if else statement

查看:1650
本文介绍了简写c ++ if else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是好奇,如果有一个简短的声明到这:

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屋!

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