' Glasses of Grape soda ' PRINT "glasses of soda =", 128 * (5 - 1.5) / 8 END
The parentheses are needed:
In the above program we relied on both parentheses and operator priority to do the arithmetic in the correct order:
128 * ( 5 - 1.5 ) / 8 = 128 * 3.5 / 8 = 448 / 8 = 56 ----------- ---------- ------- do first do next do last
Remember that since * and / have equal priority, when there are two of them in an expression the leftmost is done first.
Sometimes you would like to use parentheses to carefully show in what order the arithmetic will be done, even if the parentheses are not really needed. For example, the above program could have been written as:
' Glasses of Grape soda ' PRINT "glasses of soda =", (128 * (5 - 1.5)) / 8 END ^ ^
The two parentheses marked with a "^" are not needed. This program will calculate and print exactly the same thing as the previous program. But now the extra parentheses make clear what will be done before the division by 8 is carried out.
What will the following program print?
' Extra Parentheses ' PRINT (12 - 6 + 2) END