A good answer might be:

  • What is the absolute value of -9?
    • +9
  • What is the absolute value of +9?
    • +9

If-Else Absolute Value

If you need an absolute value, you can do this:

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
  1. The entire expression evaluates to a single value.
  2. That value will be one of two values:
    • If the condition is true, then the expression between ? and : says what value to use.
    • If the condition is false, then the expression between : and the end says what value to use.
  3. If used in an assignment statement (as above), that value is assigned to the variable.
  4. An expression using a conditional operator can be part of larger expressions.
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 is 
         true
                      ------
                      2.  this is evaluated, 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.

QUESTION 2:

Given

int a = 7, b = 21;
What is the value of:
a > b ? a : b 
(Remember, even though it looks funny, the entire expression stands for a single value.)

Click Here after you have answered the question