Combining operators in one expression
An expression is a piece of code that Kotlin evaluates to a single value. So far, each operator took two values. You can chain several operators in one expression, and Kotlin works through them in a fixed order. Numbers and variables can be mixed freely:
println(3 - 4 + 6) // 5
println(5 + 2 - 3) // 4
println(2 * 5 + 1) // 11
println(4 / 2 * 5) // 10
val num1 = 5
val num2 = -1
println(2 * num1 * num2) // -10
println(num1 + num2 - 3) // 1
println(20 / num1 + num2) // 3
println(num1 / 5 * num2) // -1
How Kotlin picks the order
Two rules decide the order, and they are enough for everything in this lesson:
*,/and%come before+and-. In2 * 5 + 1, the multiplication runs first, giving10 + 1, which is 11.- Operators at the same level run from left to right. In
3 - 4 + 6, Kotlin computes3 - 4to get -1, then adds 6 to get 5. In4 / 2 * 5it computes4 / 2first, giving 2, and then 10.
Each variable is simply replaced by its value. In 20 / num1 + num2, the division gives 20 / 5 = 4, and adding -1 gives 3.
Watch the integer division
The left-to-right rule matters most when division is involved, because dividing two Int values drops the fraction at that step. Compare these:
println(6 / 4 * 3) // 3
println(6 * 3 / 4) // 4
println(6.0 / 4 * 3) // 4.5
In the first line, 6 / 4 becomes 1 before the multiplication, so the answer is 3. In the second, 6 * 3 is 18 and 18 / 4 is 4. Same numbers, different order, different results. With a Double in the expression, nothing is dropped and the result is 4.5.
The exercise
The variables price (93) and bag (7) hold the cost of one product and one bag. The task has two steps, and each prints one line.
Step 1: Andy's total
Andy buys four products and one bag. Four products cost 4 * price, and the bag is added with + bag. The multiplication is evaluated first, so no parentheses are needed. The result, 4 times 93 plus 7, is 379.
Step 2: what Mary still needs
Mary has 150 and wants two products and one bag. Her total is 2 * price + bag, and the money she already has is subtracted at the end. Because + and - are at the same level, Kotlin goes left to right: first 2 * price, then adds bag, then subtracts 150. The total is 193, so she needs 43 more.
val price = 93
val bag = 7
println(4 * price + bag)
println(2 * price + bag - 150)
The program prints 379 and then 43. Both values are computed from the variables, so changing price or bag updates both answers. Since no type is written, Kotlin infers Int from the literals.
Parentheses and storing results
Parentheses override the default order whenever you need a different grouping. Using the same prices, 4 * price + bag pays for four products and one bag, while 4 * (price + bag) would pay for four of each:
val price = 93
val bag = 7
println(4 * price + bag) // 379
println(4 * (price + bag)) // 400
When an expression grows long, you can also break it into named pieces. This often reads better than one long line:
val total = 4 * price + bag
println(total - 150) // 229
The lesson's own tests only look at what is printed, so either style works. Choose whichever you can read most easily a week later.
Common mistakes
- Subtracting in the wrong place.
2 * price + bag - 150is correct. Writing2 * price + bag * 150would multiply the bag by 150, because multiplication binds tighter. - Splitting what belongs together. Computing
price + bagfirst and then multiplying by 4 would pay for four bags too. - Expecting division to keep decimals. With
Intvariables,/truncates at the moment it runs. - Forgetting the second
println. Each answer needs its own line of output.
Key takeaways
- Several operators can share an expression, and variables can stand in for numbers.
*,/and%run before+and-; equal-level operators run left to right.- Integer division drops its fraction as soon as it runs, so the order of operations can change the result.
Checking your own work
A reliable habit is to compute the expression by hand before running it. Write each step on its own line, for example 2 * 93 + 7 - 150 becomes 186 + 7 - 150, then 193 - 150, then 43. If the program prints something else, the mismatch usually points to a misplaced operator or to integer division dropping a fraction.