Lec 04 ConditionalStatements
Lec 04 ConditionalStatements
Autumn 2020
Operations and
Conditional
Assignments
Operator Precedence and
Associativity
An explicitly parenthesized arithmetic (and/or logical)
expression clearly indicates the sequence of
operations to be performed on its arguments.
However, it is quite common that we do not write all
the parentheses in such expressions.
Instead, we use some rules of precedence and
associativity, that make the sequence clear.
For example, the expression
a + b * c conventionally stands for
a + (b * c)
and not for (a + b) * c
Another ambiguity
Let us look at the expression a - b - c
Now the common operand b belongs to two same
operators (subtraction).
They have the same precedence. Now we can
evaluate this as
(a - b) - c or as
a - (b - c)
Again the two expressions may evaluate to different values.
The convention is that the first interpretation is correct.
In other words, the subtraction operator is left-
associative.
Associativity and Precedence
Operator(s) Type Associativity
++ -- unary non-associative
-~ unary right
+- binary left
|^ binary left
Booleaan Values in C
Instead it uses any value (integer, floating point, character, etc.) as a
Boolean value.
Any non-zero value of an expression evaluates to "true", and the zero
value evaluates to "false". In fact, C allows expressions as logical
conditions.
Example:
0 False
1 True
6 - 2 * 3 False
(6 - 2) * 3 True
0.0075 True
0e10 False
'A' True
'\0' False
x = 0 False
x = 1 True
The last two examples point out the potential danger of mistakenly
writing = in place of ==. Recall that an assignment returns a value,
which is the value that is assigned.
Logical Operators
Logical operator Syntax True if and only if
NOT !C C is false
Examples
(7*7 < 50) && (50 < 8*8) True
(7*7 < 50) && (8*8 < 50) False
(7*7 < 50) || (8*8 < 50) True
!(8*8 < 50) True
('A' > 'B') || ('a' > 'b') False
('A' > 'B') || ('A' < 'B') True
('A' < 'B') && !('a' > 'b') True
Note
Notice that here is yet another source of
logical bug. Using a single & and | in order
to denote a logical operator actually means
letting the program perform a bit-wise
operation and possibly ending up in a
logically incorrect answer
Associativity of Logical
Operators
Operator(s) Type Associativity
! Unary Right
== != Binary Left
|| Binary Left
Examples
x <= y && y <= z || a >= b is equivalent to
((x <= y) && (y <= z)) || (a >= b).
C1 && C2 && C3 is equivalent to
(C1 && C2) && C3.
a > b > c is equivalent to
(a > b) > c.
The If Statement
C Statement:
if(Condition)
Block1;
scanf("%d",&x);
if (x < 0) x = -x;
x=x+1;
The If else Statement
C Statement:
if (Condition)
{ Block 1 }
else { Block 2 }
scanf("%d",&x);
if (x >= 0) y = x;
else y = -x;
x=x+1;
Ternary Operator
Consists of two symbols: ? and :
example,
larger = (i > j) : i : j;
i and j are two test expressions.
Depending on whether i > j, larger (the variable
on the left) is assigned.
if (i > j), larger = i
else (i,e i<=j), larger = j
This is the only operator in C which takes three
operands.
The ternary statement
Consider the following special form of the if-else
statement:
if (C) v = E1; else v = E2; Here depending upon the
condition C, the variable v is assigned the value of
either the expression E1 or the expression E2. This
can be alternatively described as:
v = (C) ? E1 : E2; Here is an explicit example.
Suppose we want to compute the larger of two
numbers x and y and store the result in z. We can
write:
z = (x >= y) ? x : y;
Comma Operator
int i, j;
i=(j=1,j+10);
What is the result? j=11.
Nested If else
Suppose that we want to compute the absolute value
|xy| of the product of two integers x and y and store
the value in z. Here is a possible way of doing it:
if (x >= 0)
{ z = x;
if (y >= 0) z *= y;
else z *= -y; }
else { z = -x;
if (y >= 0) z *= y;
else z *= -y; }
This can also be implemented as:
if (x >= 0) z = x; else z = -x;
if (y >= 0) z *= y; else z *= -y;
-bash-3.2$ ./a.out
Choice of destination:
1 - Mercury
2 - Venus
3 - Mars
Enter the number corresponding to your choice: