-5

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;
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
kravien
  • 3
  • 1

1 Answers1

1

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.

laptou
  • 6,389
  • 2
  • 28
  • 59