 # Question CS101 Unit 5.3 The 'Switch' Statement

I have a question for Computer Science 101. Unit 5 , 5.3 The ‘Switch’ Statement, Central Connecticut University - Chapter 43.

If-Else Absolute Value

here is the following statement I am having problems with

double value = -34.569;
double abs;

abs = (value < 0) ? -value : value;

Question: why is +34.569 assigned to abs, and not -34.569?

A short discussion of the notion of “absolute value”.

The term “absolute value” refers to a function. We can give the function any name want. So, for example, proceeding unimaginatively we can say for the rest of this discussion that the name of the function that refers to the concept of “absolute value” is f().

Now that we have a name for the function, we have to define what it does. In mathematical notation this is the definition of f().

Defintion: For any number x, f(x) = x, if x ≥ 0, but f(x) = -x, if x < 0.

Consider the computer code :

double value = -34.569;
double abs;
abs = (value < 0) ? -value : value;

‘double value’ means that ‘value’ has been declared to be something that can contain a number. The part ‘= -34.569’ mean that ‘value’ has been defined to be the number -34.569.

‘double abs;’ means that abs has been declared to be something that can contain a number, but at this point it has not been assigned a value; that is it has not yet been defined.

‘abs = (value < 0) ? -value : value;’ is short hand for the following 4 lines of code:

if ( value < 0 )
f(value) = -value;
else
f(value) = value;

Since value = -34.569, it’s clear that value < 0. Therefor the ‘if’ clause gets executed, and f(value) = -value = -(-34.569) = =34.569. The ‘else’ clause simply gets ignored.

Now look again at the above definition to see how this all ties together.

I don’t know who decided that f(x) should be referred to as |x|.