I use ternary operator ?: in C++ a lot. Unfortunately I never went further to use it on multiple branches. Today I found that it is legal to write
char* str = size < 10 ? "small" :
size < 20 ? "medium" :
size < 30? "large" :
"extra large";
It is equivalent as:
if ( size < 10 )
str = "small";
else if (size <20)
str = "medium";
else if (size < 30)
str = "large";
else
str = "extra large";
Nicer, uuuhhh
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment