posted 17 years ago
Hi Yogaraj,
In case you don't already know, the ternary conditional operator ( ? : ) is basically a shorthand for an assignment based on a if-else construct. For instance,
is behaviorally equivalent to
where condition is a boolean expression and v1, v2 have types that are assignable to a. Also, v1 is evaluated only if condition is true, and v2 is evaluated only if condition is false. The main advantages of the ?: operator are that (a) it's often more concise than the equivalent if-else construct; and (b) it's not limited to assignment statements, and can be used within another expression (as your example illustrates).
Another thing to realize is that the ?: operator is right-associative, like the assignment operator (=) but unlike the usual arithmetic operators. What this means is that "c ? v1 : c2 ? v2 : v3" is equivalent to "c ? v1 : (c2 ? v2 : v3)" and not "(c ? v1 : c2) ? v2 : v3".
The above should be all you need to figure out why your code produces the output you see. Good luck!
[ October 31, 2007: Message edited by: Kelvin Lim ]