Chapter 4: C Types, Operators, and Expressions
external conditions. For
xample, if the tem ° F, turn the fan on, if it is under 100° F,
d write this as:
Fan(OFF);
r you could use the C conditional operator ?: (
able 3
temp > 150? Fan(ON) : Fan(OFF);
he op rm: expresson1? expression2 : expression3, and follows
ut you’ll see this expression a
hen a sequence of operators such as:
The co er of calculation based on operator precedence (Table
7). But y not be what you intended. Calculate the
value o. D ons quentially as
listed you get:
10 / 2 – 20 * 4
x = 40
Conditional Expressions................................................................................
You will frequently need to make decisions based on
e perature is above 150
turn the fan off. You coul
if( temp > 150)
Fan(ON);
else
O
T ) as below:
T eration has the fo
the rule that if expression1 is true (non-zero value) then use expression2,
otherwise use expression3. This operator seems a little gee-wiz-impress-your-
friends and not as clear as the if-else expression, b
lot, so get used to it.
Precedence and Order of Evaluation.............................................................
W statement has a
x = 50 + 10 / 2 – 20 * 4;
mpiler follows an ord
what the compiler does, ma
f x id you get 40? If you performed the calculati se
x = 50 +
x = 60 / 2 – 20 * 4
x = 30 – 20 * 4
x = 10 * 4