Hi,
How this expression will be evaluated??? help me out
Code:((a>b)?((a>c)?a:c):((b>c)?b:c))
Hi,
How this expression will be evaluated??? help me out
Code:((a>b)?((a>c)?a:c):((b>c)?b:c))
Quickly.Originally Posted by rits
If you want to know what the expression does, analyse it yourself following what you know of the ternary operator.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
the ?: operator is directly related to if..else statements. convert one to the other and you know how it works.
Code:// ((a>b)?((a>c)?a:c):((b>c)?b:c)) if (a > b) { if (a > c) a; else c; } else { if (b > c) b; else c; }
Thanks Meldreth very clear explanation...
It's worth noting that the ternary operator evaluates to a value, unlike if-else statements; so, for example, you can do something like this . . .
Then sign is assigned the value -1 if number is negative, and +1 otherwise.Code:sign = (number < 0 ? -1 : 1);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
According to my limited thinking thats also if-else. if number is less than 0 sign is -1 otherwise 1
Yes, what dwks was trying to say is that the use of the ternary operator is slightly different from an if (although it has the same effect).
You can not do:
That will not compile.Code:x = if(a < b) a; else b;
You could do:
or you can use the ternary form:Code:if (a < b) x = a; else x = b;
The latter comes in very handy if you have a long expression:Code:x = (a < b)?a:b;
If-statements:
would be a bit easier on the hands typing and the eyes reading it like this:Code:if (a < b) x = 3.6 * y + 9.6 *b; else x = 3.6 * y + 9.6 * a;
Note that there are situations when this is RIGHT, and other times when it is wrong. It may be better to have a temporary variable:Code:x = 3.6 *y + 9.6 * (a < b)?b:a;
Code:double bigger = a; if (a < b) bigger = b; x = 3.6 * y + 9.6 * bigger;
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The conditional evaluation is a compact way of expressing an if-else.
As I tried to say, the conditional operator is pretty much the same thing as an if-else statement. However, the ternary operator evaluates to a value, whereas an if-else construct does not. Whether you use this value is up to you.
You can always convert a ternary operator into an if-else, but it's not quite as simple as Meldreth has made out if the value of the expression is used. That's all I was trying to say, and matsp has elaborated nicely where I was too lazy to.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Hey buddies thanks to all of you