Variables and Assignment Statements

27. Extra Parentheses


Answer:

Expression
(8 - 2) / 2
(2 + 6) / 2 - 9
(8 + 4) * 2
8 + 4 * 2
8 + (4 * 2)
Value
3
-5
24
16
16

Extra Parentheses

Notice that the last expression (above) did not need parentheses. It does not hurt to use parentheses that are not needed. For example, all of the following are equivalent:

Equivalent Expressions
a + b + c * d
a + b + (c * d)
(a + b) + (c * d)
((a + b) + (c * d))


Warning!   Look out for division. The / operator is a constant source of bugs! Parentheses will help.


Question 27:

What is the value of each of the following expressions?

Expression
8 + 2 / 2 + 3
(8 + 2) / (2 + 3)
(8 + 2) / 2 + 3
8 + 2 / (2 + 3)
Value