Start learning free

LESSON 18 OF 348 · CALCULATIONS WITH NUMBERS

Mathematical Expressions in Kotlin

Lesson Read the full explanation

  • We can perform multiple calculations using operators in the same line of code:

    println(3 - 4 + 6) // Prints 5
    println(5 + 2 - 3) // Prints 4
    println(2 * 5 + 1) // Prints 11
    println(4 / 2 * 5) // Prints 10
    

  • We can also use variables:

    val num1 = 5
    val num2 = -1
    
    println(2 * num1 * num2) // Prints -10
    println(num1 + num2 - 3) // Prints 1
    println(20 / num1 + num2) // Prints 3
    println(num1 / 5 * num2) // Prints -1
    



Assignment
Follow the Coding Tutorial and let's practice with mathematical expressions!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS WITH NUMBERS

    Mathematical Expressions in Kotlin, explained

    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:

    1. *, / and % come before + and -. In 2 * 5 + 1, the multiplication runs first, giving 10 + 1, which is 11.
    2. Operators at the same level run from left to right. In 3 - 4 + 6, Kotlin computes 3 - 4 to get -1, then adds 6 to get 5. In 4 / 2 * 5 it computes 4 / 2 first, 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 - 150 is correct. Writing 2 * price + bag * 150 would multiply the bag by 150, because multiplication binds tighter.
    • Splitting what belongs together. Computing price + bag first and then multiplying by 4 would pay for four bags too.
    • Expecting division to keep decimals. With Int variables, / 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.

    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free