[c++] 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;

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.

Thanks!

I actually just figured it out right before you guys had answered.

I'm using bigInt.sign = (number < 0) ? 1 : 0

This question is related to c++ if-statement

The answer is


try this:

bigInt.sign = number < 0 ? 0 : 1

The basic syntax for using ternary operator is like this:

(condition) ? (if_true) : (if_false)

For you case it is like this:

number < 0 ? bigInt.sign = 0 : bigInt.sign = 1;

Depending on how often you use this in your code you could consider the following:

macro

#define SIGN(x) ( (x) >= 0 )

Inline function

inline int sign(int x)
{
    return x >= 0;
}

Then you would just go:

bigInt.sign = sign(number);