|
|
|
|
|
by pestaa
4809 days ago
|
|
I find the ternary expressions very easy to read and understand and never understood why others find it difficult at all. I see no semantical difference between if(expr1) expr2 else expr3;
and expr1 ? expr2 : expr3;
Okay you need to learn the syntax, but it should be second nature after a few times. I also think the question mark is very intuitive. |
|
If-Then-Else is a statement. It doesn't return a value. It's just a branching operation between the two blocks.
The conditional operator is an expression. It returns a value hence it has a type. And that's where things start to be messy. Typing rules for the conditional operator are, well, not trivial and varie from language to language. Let's use an exemple from Java Puzzlers (Joshua Bloch and Neal Gafter) :
char x = 'X'; int i = 0; System.out.print(true ? x : 0); System.out.print(false ? i : x);
will print X88 but the same thing in C++
will print 8888.And I am not even talking about the priority mess when you try to use a conditional operator in a conditional operator.