I think it's because of code readability.
Consider this two codes, where a
is a number (int
, float
, or whatever):
#1
if(a < 0) {
a = 0;
} else if(a < 10) {
a = 10;
} else if(a < 20) {
a = 20;
} else if(a < 30) {
a = 30;
}
#2
a = a < 0 ? 0 : a < 10 ? 10 : a < 20 ? 20 : a < 30 ? 30 : a;
They do exactly the same thing, but the first one is more clear.