Page 156 - Cs_withBlue_J_C11_Flipbook
P. 156
Some Solved Examples:
1. int m=10, n=20, p=12, q=4, e;
e = m - n * (q % p) + 10 * q;
= 10 - 20 * (4 % 12) + 10 * 4 [innermost bracket]
= 10 - 20 * 4 + 10 * 4 [multiplication]
= 10 - 80 + 40 [subtraction]
= -70 + 40 [addition]
= -30
Hence, e=-30 (Answer)
2. y += ++y + y-- + --y; when int y=8
y = y + (++y + y-- + --y) [bracket]
= 8 + (9 + 9 + 7) [prefix and postfix]
= 8 + (25) [addition]
= 33
3. boolean result = (4<5) && !(2<3) || (7==7));
Output: true
Note: Since the relational operator has higher precedence than the logical operator, the relational
operator will be executed before the logical operator.
The precedence of logical operators is as follows:
NOT(!), AND (&&) and OR (!!)
So, if a statement contains all the three logical operators, then NOT operator will be operated first.
Examples:
1. (2<3) = true
!(2<3) = false
2. (4<5) = true
3. (4<5) && !(2<3) = false
4. (7==7) = true
5. (4<5) && !(2<3) || (7==7)) = true
Hence, the result is true.
7.4 ASSOCIATIVITY OF OPERATORS
Some operators have the same precedence. In that case, the evaluation is done according to their associativity, which
is either from left to right or vice-versa.
Suppose, in an expression “a+b-c”, the operators + and - are given without any parenthesis. Since the binary operators
have left-to-right associativity, the addition will be executed before the subtraction.
Similarly, in an expression “a%b*c”, the operators % and * are given without any parenthesis and the modulus will be
executed before the multiplication.
The associativity of some operators is as shown below.
Hierarchy Order Operators Associativity of the Operators
1 postfix unary Right to Left
2 unary including prefix, logical NOT Right to Left
3 multiplication, division and modulus Left to Right
4 addition and subtraction Left to Right
154154 Touchpad Computer Science-XI

