Start learning free

LESSON 18 OF 349 · CALCULATIONS WITH NUMBERS

Mathematical Expressions in Swift in Swift

Lesson

  • 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

    Mathematical Expressions in Swift

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