Conditional/Ternary Operator ?

Share:

Conditional/Ternary Operator ?:

A ternary operator operates on 3 operands which can be used instead of a if...elsestatement.
Consider this code:
if ( a < b ) {
   a = b;
}
else {
   a = -b;
}
You can replace the above code with:
a = (a < b) ? b : -b;
The ternary operator is more readable than a if...else statement for short conditions.