Start learning free

LESSON 18 OF 415 · CALCULATIONS WITH NUMBERS

Mathematical Expressions in Swift: Combining Operators

Lesson Read the full explanation

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

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

  • We can also use variables:

    let num1 = 5
    let num2 = -1
    
    print(2 * num1 * num2) // Prints -10
    print(num1 + num2 - 3) // Prints 1
    print(20 / num1 + num2) // Prints 3
    print(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

    SWIFT · CALCULATIONS WITH NUMBERS

    Mathematical Expressions in Swift, explained

    What a mathematical expression is

    An expression is a piece of code that produces a value. 3 - 4 + 6 is an expression, and so is 2 * price + bag. You can chain as many operators as you like in one expression, and you can mix numbers with constants and variables. Swift computes the whole thing and gives you one result, which print can show.

    Several operators in one line

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

    Two rules decide the order of the work:

    • *, / and % are applied before + and -. In 2 * 5 + 1 the product 10 is computed first, then 1 is added.
    • Operators at the same level are applied from left to right. In 4 / 2 * 5 the division comes first (giving 2), then the multiplication (giving 10). In 3 - 4 + 6 the subtraction comes first (giving -1), then 6 is added.

    Using constants and variables

    Names stand for their values, so an expression with names works exactly like one with numbers:

    let num1 = 5
    let num2 = -1
    
    print(2 * num1 * num2)   // -10
    print(num1 + num2 - 3)   // 1
    print(20 / num1 + num2)  // 3
    print(num1 / 5 * num2)   // -1
    

    The third line is 20 / 5 + (-1), which is 4 - 1. The fourth is 5 / 5 * (-1), which is 1 * (-1). A negative value stored in a constant is just a number, so there is no need for parentheses around num2.

    Whole numbers divide as whole numbers

    All the values above are Int, and dividing two Int values gives an Int: the fractional part is dropped. 7 / 2 is 3, not 3.5. This matters inside longer expressions. 8 / 3 * 3 is 2 * 3, which is 6, while 8 * 3 / 3 is 24 / 3, which is 8. The same numbers in a different order can give different answers.

    When an expression gets long, add parentheses or split it over several constants to make your intent obvious. Swift doesn't require either, but a reader shouldn't have to recall the precedence rules to follow your code. The next lesson covers precedence in detail.

    The exercise

    The starter code holds the price of one product and the price of one bag:

    let price = 93
    let bag = 7
    

    Step 1: Andy's total

    Andy buys four products and one bag. The cost is four times the price plus the bag. Because multiplication happens before addition, 4 * price + bag needs no parentheses: it computes 372 and then adds 7.

    Step 2: how much more Mary needs

    Mary wants two products and one bag, and she has 150 dollars. Her total is 2 * price + bag, which is 193. Subtracting what she has gives the missing amount. Because + and - are applied from left to right, the expression 2 * price + bag - 150 builds the total first and subtracts 150 afterwards.

    The solution in Swift

    let price = 93
    let bag = 7
    
    print(4 * price + bag)
    print(2 * price + bag - 150)
    

    The program prints two lines:

    379
    43
    

    Common mistakes

    • Adding before multiplying by mistake. Writing 4 * (price + bag) would give 400, which is the cost of four products and four bags.
    • Subtracting too early. 2 * price + (bag - 150) happens to give the same 43 here, but it describes a different idea. Build the total, then subtract the money Mary has.
    • Using x or × for multiplication. Swift only understands *.
    • Mixing Int and Double. Swift doesn't convert between them silently. With let rate: Double = 2.5, the expression price * rate doesn't compile because price is an Int.

    Key takeaways

    • One expression can contain many operators and many names.
    • *, / and % run before + and -; equal levels run left to right.
    • Dividing two Int values drops the fraction, so the order of operations can change the result.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free