4.8 Conditional operator
Quite often it is necessary to decide which part of the code of a
script to execute and have this decision depend on the result of a
calculation or external input. For these cases a construct is helpful
that allows to tell the program to do something if e.g. the result of
a calculation is less then 0
and to do something else if the
result was larger or equal to 0
. An admittedly somewhat
contrived example would be the calculation of the square root of a
number, making sure the number is multiplied by -1 before calculating
the root in case it's negative. Using the conditional operator this can
be written as
sqrt_of_val = ( val < 0.0 ) ? sqrt( -1.0 * val ) : sqrt( val ); |
The conditional operator is somewhat special in that it consists of both
the question mark, ?
, and the colon, :
. It tells the
program to evaluate the expression before the question mark and then,
depending on the result, execute either the expression before or after
the colon. In the example it is tested if val
is smaller than
0
, and if this is true, the square root of val
multiplied
by -1
is taken, otherwise the square root of just val
itself.
Beside comparison for the number on the left hand side being smaller than the number on the right hand side, several other operators for comparisons exist. Now follows a complete list of such comparison operators, which can only be applied to expressions that result in numbers, e.g. either simple numbers, numerical variables or numerical results of a calculation, but never to arrays:
==
true if left hand side is equal to the right hand side !=
true if left and right hand side are unequal <
true if left side is less than right side <=
true if left side is less or equal to right side >
true if left side is greater than right side >=
true if left side is greater or equal to right side
Please note that using comparisons between floating point numbers may
not work as you may expect them to do. This is a result of the finite
precision of floating point numbers. Thus, for example, 0.1
and
the result of e.g. 0.7 / 7
can have different values, in which
case a comparison for equality will fail! Thus comparison for equality
should in principle only be used with integer numbers.
All these comparison operators actually do a calculation, resulting in a
new number. When you see something like a <= b
this evaluates to
1
if the expression is true (i.e. if the value of a
is
less or equal to the value of b
), and otherwise to 0
. In
the test of the conditional execution operator (i.e. everything before
the question mark) it is checked if the result is a non-zero number,
which then is taken as true, or if it's zero, which is interpreted as
meaning false. Thus instead of an expression with a comparison operator
the test part could also consist of just a simple number and what is
going to be executed of the two alternatives after the question mark
depends on the value of this number: if it is non-zero the first
alternative is used, but if it is zero the second. Thus it would be
valid to write:
exp_of_val = val ? exp( val ) : 1; |
This checks if val
is non-zero, in which case the exponential of
val
is calculated by doing a function call, otherwise, if
val
is 0
, the correct result of 1
is written out
directly instead of using a slightly slower function call.
One has to be a bit careful when using the operator for conditional execution because it has a very low precedence. After
x1 = 1.0 + val < 0 ? 5 : 9; x2 = 1.0 + ( val < 0 ? 5 : 9 ); |
x1
and x2
will have different values. In the first line it
is checked if val
plus 1.0
is smaller than 0
and
x1
is set accordingly to either 5
or 9
, while in
the second line it is checked if val
is less than 0
and,
depending on the result, either 5
or 9
is added to
1.0
, resulting in 6
or 10
. Also the following two
assignments may result in different values, at least if val
is
negative:
x1 = val < 0 ? 5 : 9 + 1; x2 = ( val < 0 ? 5 : 9 ) + 1; |
The value of x1
will be either set to 5
or 10
(the
sum of 9
and 1
, while x2
will be set to 6
or
10
. Thus it is to be recommended to use parentheses to make sure
that the evaluation happens as expected except in trivial cases.
Beside comparison operators also logical operators can be used in the
test part of the condition operator. They are discussed in detail in the
next section, but they work very similarly to the comparison operators
in that they always result in a value of either 1
or 0
,
standing for true or false, respectively.
Finally, the same effect as with the conditional operator can also be
achieved by using an IF-ELSE
construct (see below), but only
within the EXPERIMENT
section of an EDL
script -
everywhere else only the conditional operator can be used.
This document was generated by Jens Thoms Toerring on September 6, 2017 using texi2html 1.82.