How does it work? Which one runs first?
Given
int a = 100;
What would the following return?
int b = a > 5 ? a > 10 ? 10 : 5 : 0;
How does it work? Which one runs first?
Given
int a = 100;
What would the following return?
int b = a > 5 ? a > 10 ? 10 : 5 : 0;
You can rewrite what you just wrote like this:
a > 5 ? ( a > 10 ? 10 : 5 ) : 0
After all, there is no other way you could arrange the parentheses that would make sense. Therefore, the a > 5
comparison must be tested first because if a > 5
does not evaluate to true
, then the second comparison is not necessary.