Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

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.

The shortest solution is bigInt.sign = (number < 0) ? 0 : 1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...