C++ ternary operator and an if else statement

Started by
6 comments, last by Xeee 20 years, 2 months ago
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..
xee..
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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.
// Clunkyint x;if(somecondition)    x = 10;else    x = 15;// More elegant, IMO, and certainly shorterint x = (somecondition)? 10 : 15;


[edited by - Miserable on February 6, 2004 12:43:12 AM]
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.
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.
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.
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]
SilverFan and Programmer of GamesDefender of Freedom
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)
"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

This topic is closed to new replies.

Advertisement