In: Computer Science
Show step-by-step how Java will evaluate the following expression:
8 % 3 + 3 % 2 – 9 % 2.0 * 5
15/15 % 7 *2 – 11. /4 * 5
Solution:
===============
a) Here we have % , + , - , * operator
as per Precedence rule %, * will be evaluated first and then + ,- will be evaluated.
also these expression will be evaluated form left to right.
Hence first of all 8%3 will be evaluated which return 2
then 3 % 2 will be evaluated which return 1
then 9 % 2.0 will be evaluated which return 1.0
then 1.0*5 will be evaluated which return 5.0
finally +,- operation will be performed on these result
2 + 1 - 5.0 here first 2+1 will be performed then 3-5.0 will performed hence result is -2.0
b) Here we have /,% , - , * operator
as per Precedence rule %, * , / will be evaluated first and then - will be evaluated.
also these expression will be evaluated form left to right.
Hence first of all 15/15 will be evaluated which return 1
then 1%7 return 1
then 1*2 return 2
then 11./4 return 2.75
then 2.75*5 will return 13.75
finally 2 - 13.75 will be evaluated which return -11.75 as result