iOS Swift Basics Swift Operators Working With Operators

Alison King
Alison King
iOS Development with Swift Techdegree Student 424 Points

Arithmetic Operators

in the following calculation:

let someOperation = 20 + 400 % 10 / 2 - 15

is the order of the arithmetic operators 420 divided by 5 (ie 10 over 2) and then minus 15? or 42 divided by -13 (ie 2-15)?

What are the rules as in algebra there would be brackets to specify order of the calculations

operators.swift
// Enter your code below
let value = 200
let divisor = 5

let someOperation = 20 + 400 % 10 / 2 - 15
let anotherOperation = 52 * 27 % 200 / 2 + 5

// Task 1 - Enter your code below
let result: Double = value % divisor

// Task 2 - Enter your code below

1 Answer

Alex Koumparos
MOD
Alex Koumparos
Python Web Development Treehouse Moderator 33,515 Points

Hi Alison,

Apple has a page showing operator precendence.

As per that article, the order of operations is:

10 / 2  // 5
400 % 5  // 0
20 + 0  // 20
20 - 15 // 5

Like in regular algebra you can use parentheses to override the built-in operator precedence.

Hope that helps

Cheers

Alex