Ternary operators

Started by
1 comment, last by Peaceman 21 years, 10 months ago
Do (or should) you use them? Are the faster than normal if/else statements? thx for any replys
Advertisement
Not faster, but sometimes easier to read/understand. Stuff like checking for the max of two numbers (a > b ? a : b), that sorta thing works wonders for readability. But using it in place of a general if-then statement can make code harder to read as well. My suggestion: use them if you''re confortable with them, and preferably only when they improve readability/ease of understanding of the code''s purpose.
quote:Original post by Melraidin
Not faster, but sometimes easier to read/understand.


If your CPU architecture supports predicated operations, then it may be faster, as it is 'more obvious' to the compiler what to do with it.

Be aware of the constraints of the operator though. For instance, the expression (bool)b:‌(double)d?(long)l is always of type double.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 7, 2002 11:36:33 AM]
"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