Assume the following rules of associativity and precedence for expressions: Prec
ID: 3715878 • Letter: A
Question
Assume the following rules of associativity and precedence for expressions:
Precedence
Highest
*, /, not
+, , &, mod
(unary)
=, /=, < , <=, >=, >
and
or, xor
Lowest
Associativity Left to right
Show the order of evaluation of the following expressions by parenthesizing all subexpressions and placing
a superscript on the right parenthesis to indicate order. For example, for the expression a + b * c + d
the order of evaluation would be represented as
((a + (b * c)1 )2 + d)3
a. a * b - 1 + c
b. a * (b - 1) / c mod d
c. (a - b) / c & (d * e / a - 3)
d. -a or c = d and e
e. a > b xor c or d <= 17
f. -a + b
Explanation / Answer
Associativity and precedence of operators :
Precedence
Highest
*, /, not
+, , &, mod
(unary)
=, /=, < , <=, >=, >
and
or, xor
Lowest
Associativity Left to right
a) a*b -1 +c
* has higher precedence than - and +
Associativity is from left to right
Therefore, after parenthesizing expression becomes :
( ( ( a*b )1-1 )2 + c )3
b) a*(b-1)/c mod d
* and / has same precendence but associativity is from left to right
mod has lower precedence than * , /
After parenthesizing expression becomes :
( ( (a* (b-1)1 )2 / c)3 mod d)4
c) (a-b) / c & (d * e / a - 3 )
Precedence of operators is *,/ then - , &
After parenthesizing expression becomes :
( ( (a-b)1 / c)2 & ( ( (d * e)3 / a)4 - 3 )5)6
d) -a or c = d and e
unary operator has higher precedence than = and then comes 'and' and finally 'or'
After parenthesizing expression becomes :
( (-a)1 or ( ( c = d )2 and e )3)4
e) a > b xor c or d <= 17
After parenthesizing expression becomes :
( ( ( a > b )1 xor c)3 or ( d <= 17 )2 )4
f) -a + b
After parenthesizing expression becomes :
( - ( a + b )1 ) 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.