Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Xeee

C++ ternary operator and an if else statement

This topic is 5341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what''s the difference between them, is there any reason to use one of them over the other? (like being faster for example). thanks in advance xee..

Share this post


Link to post
Share on other sites
Advertisement
You can use the ternary operator in initializer lists. But both expand out to the same machine code. I don''t touch the ? operator much because it always makes me stop and think too long.

Share this post


Link to post
Share on other sites
The ternary operator results in an expression that evaluates to a value, so it can be used in initialiser lists (as mentioned), assignments, function calls, and whatever other situations you might encounter where you need conditional expressions to evaluate to something.

// Clunky

int x;
if(somecondition)
x = 10;
else
x = 15;

// More elegant, IMO, and certainly shorter

int x = (somecondition)? 10 : 15;


[edited by - Miserable on February 6, 2004 12:43:12 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by antareus
You can use the ternary operator in initializer lists. But both expand out to the same machine code. I don''t touch the ? operator much because it always makes me stop and think too long.

I disagree; this operator is exactly like a shortened if/else statement. When I write them, I think "if true, then die or else don''t", which very easily translates to true ? die : don''t.

Share this post


Link to post
Share on other sites
quote:
Original post by psykr
quote:
Original post by antareus
You can use the ternary operator in initializer lists. But both expand out to the same machine code. I don''t touch the ? operator much because it always makes me stop and think too long.

I disagree; this operator is exactly like a shortened if/else statement. When I write them, I think "if true, then die or else don''t", which very easily translates to true ? die : don''t.


Do you mean you disagree that it takes too long to think about? Antareus isn''t wrong about the initialiser list. The other is just personal experience which you can''t really disagree with.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by psykr
I disagree; this operator is exactly like a shortened if/else statement. When I write them, I think "if true, then die or else don''t", which very easily translates to true ? die : don''t.


This is incorrect. Miserable is right. The difference is that if/else is a statement, but the ternary operator ?: is an expression. As a slight twist on Miserable''s code, try getting this to work:


int x, y=6;
x=if (y>7) 5 else 7 ;


It''s not going to happen. Although similar to if/else, ?: is a bit different. Note that an if statement takes a statement (including compound statements) as the "target" of its truth condition. Also note that an if statement does not require an else whereas ?: always requires both a true and a false expression.

Share this post


Link to post
Share on other sites
The ternary operator can help create more elegent code on function calls. For instance:


int x;
x = SomeFunction(Arg1, Arg2, y ? Arg3 : Arg4, Arg5);
//instead of...

if (y)
x = SomeFunction(Arg1, Arg2, Arg3, Arg5);
else
x = SomeFunction(Arg1, Arg2, Arg4, Arg5);


The other place I like to use the ternary operator is on return statements:

   
return WasError ? E_FAIL : S_OK;
//instead of...

if (WasError)
return E_FAIL;
return S_OK;


This is kind of a stylistic thing, but personally I think it looks neater. Also, I had a computer science teacher who swore up and down that the ternary operator was slighter faster, but I can't speak to this from personal experience.

[edited by - GD-Silver on February 7, 2004 7:12:37 PM]

[edited by - GD-Silver on February 7, 2004 7:13:25 PM]

Share this post


Link to post
Share on other sites
One is used in expressions, the other is a statement.
That''s all you really need to know.
There are no differences in performance whatsoever.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!