Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics Swift Operators Working With Operators

Alison King
seal-mask
.a{fill-rule:evenodd;}techdegree
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
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 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