What an expression is
An expression is any piece of code that Scala can evaluate to a value. 3 - 4 + 6 is an expression, and so is 2 * num1 * num2. You can chain as many operators as you like on one line, and Scala works out a single result. Put the expression inside println to see it:
println(3 - 4 + 6)
println(5 + 2 - 3)
println(2 * 5 + 1)
println(4 / 2 * 5)
5
4
11
10
Variables can take part as well. Scala substitutes each variable's current value:
var num1 = 5
var num2 = -1
println(2 * num1 * num2)
println(num1 + num2 - 3)
println(20 / num1 + num2)
println(num1 / 5 * num2)
-10
1
3
-1
The order of evaluation
Two rules decide what happens first in a longer expression.
- Multiplication, division and remainder come before addition and subtraction. In
2 * 5 + 1, the product 10 is computed first, then 1 is added. - Operators of the same rank run from left to right. In
3 - 4 + 6, Scala computes3 - 4as -1 and then adds 6.
The second rule matters most with subtraction and division, where the order changes the answer. Compare these:
println(10 - 4 - 3)
println(10 - (4 - 3))
println(100 / 10 / 5)
println(100 / (10 / 5))
3
9
2
50
Without parentheses, 10 - 4 - 3 is (10 - 4) - 3. Parentheses let you force a different grouping.
Integer division inside an expression
When every number is an Int, each / drops its fraction at once, in the middle of the calculation, not at the end. That makes the order of the operators visible:
println(4 * 5 / 2)
println(5 / 2 * 4)
println(1 / 2 * 10)
println(10 * 1 / 2)
10
8
0
5
The first two lines use the same numbers in a different order. In 5 / 2 * 4, the division runs first and gives 2 (not 2.5), so the result is 8. In 4 * 5 / 2, the multiplication runs first, so no precision is lost. When you work with whole numbers, multiply first and divide last if you want the most exact answer.
The exercise
The starter code gives two values: price is the cost of one product and bag is the cost of one bag. There are two printed answers.
Step 1: Andy's total
Andy buys four products and one bag. That is four times the price plus the bag. Since multiplication comes first, 4 * price + bag needs no parentheses. With a price of 93 and a bag of 7, the total is 4 * 93 + 7 = 379.
Step 2: what Mary still needs
Mary wants two products and one bag, and she already has 150 dollars. Build her total first, then subtract the money she holds: 2 * price + bag - 150. The multiplication runs first, then the addition and the subtraction go from left to right, so the total is complete before 150 is taken away. That is 2 * 93 + 7 = 193, and 193 - 150 = 43.
val price = 93
val bag = 7
println(4 * price + bag)
println(2 * price + bag - 150)
The program prints:
379
43
Common mistakes
- Parentheses in the wrong place.
2 * (price + bag) - 150pays for two bags. For these values it gives 50, not 43. - Typing the numbers instead of the names. Use
priceandbag, so the answer follows the values. - Subtracting in the wrong direction. The amount she still needs is the total minus what she has, not the other way round.
Key takeaways
- One expression can hold many operators and variables.
*,/and%are evaluated before+and-; ties go left to right.- With
Intvalues, every division drops its fraction immediately.
