Tertiary Operator
by ilyas usal
This operator is also used to shorten if tests. It is used in the case
that: if something is true, then 1 (or 0) thing happens, otherwise 1 (or 0)
thing happens. Usually, we would write it like this:
if (i<5)
{
i++;
}
else
{
i--;
}
The first thing we can improve concerns the brackets: when there's only 1
instruction after a if or a else, you can forget about them:
if (i<5) i++;
else i--;
The next step is to use the tertiary operator: A?B:C, which means "if A is
true, then B, else C". In our previous example, it would give:
(i<5)?i++:i-- ;
If you don't want anything to happen, replace B or C by
null. If you want something more complex to
happen, replace B or C by a function.