One way to compute an absolute value is:
if ( value < 0 ) abs = -value; else abs = value;
This is awkward for such a simple idea. The following does the same thing in one statement:
abs = (value < 0 ) ? -value : value ;
The right side of the = uses a conditional operator.
In general, it looks like this:
true-or-false-condition ? value-if-true : value-if-false
Here is how it works with the above example:
double value = -34.569;
double abs;
// compute absolute value of value
abs = (value < 0 ) ? -value : value ;
------------- ------
1. condition 2. this is evaluated,
is true to +34.569
----
3. The +34.569 is assigned to abs
The conditional expression is a type of expression---that is, it asks for
a value to be computed but does not by itself change any variable.
In the above example, the variable value is not changed.