Page 485 - Introduction to Programming with Java: A Problem Solving Approach
P. 485

                11.8 Expression Evaluation Review 451 Try to evaluate the following expressions on your own prior to looking at the subsequent answers.
1. (c + a / b) / 10 * 5 2.a + b++
3.4 + --c
4. c = b = a % 2
Here are the answers:
1.
(c + a / b) / 10 * 5 ⇒ (3.0 + 5 / 2) / 10 * 5 ⇒ (3.0 + 2) / 10 * 5 ⇒
   Mixed expression—the int gets promoted to a double.
            5.0
0.5
2.5
2.
a
/ 10 * 5 ⇒ * 5 ⇒
b++ ⇒ 2⇒
--c ⇒ 2.0 ⇒
b = a % 2⇒ b = 5 % 2⇒ b = 1⇒ 1⇒
Apago PDF Enhancer
   / and * have same precedence. Perform left operation first.
     +
Use b’s original value of 2 in the expression. Afterwards, b’s value increments to 3.
  5
7
+
3.
4
+
   c’s value decrements to 2.0 before using it in the expression.
  4
+
6.0
4.
c
=
      Don’t plug in values for variables that are at the left of assignments.
     c
=
      c
c
=
=
Theb = 1assignment evaluates to 1.
         c is a double, so the result is a double.
  1.0
 






















































   483   484   485   486   487