Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
How do we know which operators get executed first when there's more than one in a single expression? Swift has a set of rules, known as operator precedence, that decides which code gets run first. In this video, we go over the precedence rules governing the operators we just learned.

0:00
All the examples we explored in our previous video gave us a good idea

0:04
of how to use the basic arithmetic operations.

0:08
But what happens when you go beyond basic math?

0:11
What if you want to apply a complex geometric or algebraic formula?

0:16
You're probably wondering what this has to do with building an app of any sort.

0:20
Well, when we build an app there's usually animation for

0:24
which you need to apply math functions.

0:26
And this knowledge may then come in handy.

0:29
Let's start off like before by adding a new comment marker.

0:33
So we'll add a multi line comment and we'll say,

0:37
we'll mark this section off as operator precedence.

0:46
Gotta make things look nice, there we go.

0:49
Let's write out a fairly complicated arithmetic operation.

0:53
So we'll say var x equal 100

0:59
plus 100 minus 5 times

1:04
2 divided by 3 modulo 7.

1:09
In the results pane, you can see the result of the expression as 197.

1:13
Let's go from left to right and carry out each step.

1:18
So 100 plus 100 equal 200.

1:22
200 minus 5 is 195.

1:25
195 divided by 2 or multiplied by 2, 390.

1:30
390 divided by 3 is 130 and then 130 modulo 7 is 4.

1:36
It is obviously not the same as the result in the playground, so what's going on?

1:41
Some operators take higher precedence than others.

1:45
When the system looks at a line of code like ours,

1:48
it knows which operations to perform first and which to perform next.

1:53
Let's take the following operators.

1:55
So we have the multiplication, division, remainder or

1:59
modulo operator, the addition and subtraction operators.

2:03
Swift assigns a priority or precedence to each of these operators.

2:08
Multiplication, division, and

2:10
the remainder operator have the same priority level at 150.

2:14
Whereas addition and subtraction have a priority level of 140.

2:19
So given a couple operators in a statement or

2:22
an expression like ours, those with the higher precedence are executed first.

2:28
Okay, but what about when the operators have the same priority?

2:32
Like 100 plus 100 minus 5?

2:35
The addition and subtraction operators both have a precedence level of 140.

2:40
It's actually quite simple, all these operators work from left to right.

2:44
Now to make it easier to read, we can try grouping them with parentheses so

2:49
that we can understand how this is calculated.

2:51
So we'll say 100 plus 100, well that's not executed first.

2:56
The statement that's executed first over here is

2:59
the one containing the multiplication, division, and remainder operators.

3:05
So let's group these, we'll put a parentheses around here.

3:09
This will happen first because since all these three have the same precedence,

3:14
we go from left to right.

3:16
Then after 5 times 2, we're going to execute 5 times 2, then divide that by 3.

3:21
And then finally, we'll execute the modulo operator.

3:25
So now when we go through this, this happens first,

3:29
this happens second, this happens third, then that.

3:33
And then finally, we subtract that value, or this value,

3:36
from the result of the first two.

3:38
Okay so working our way from the left, let's carry out these operations for

3:42
any operator that has a precedence of 150.

3:44
First we multiply 5 by 2 then we divide the resulting value by 3.

3:50
Finally, we get the remainder of that resulting value and

3:54
what is left is then executed again from left to right.

3:57
So we add 100 to 100 and then we subtract 3.

4:03
You might feel that it's overwhelming for now because it's a lot of information.

4:07
But don't worry because you don't need to memorize any of this.

4:11
Operator precedence is definitely not something you need to know

4:14
off the top of your head and very rarely is ever used in practice.

4:19
Even if you did need to know them, there are reference links in the notes section

4:22
that you can bookmark so that you can refer to them any time you want.

4:26
A programmer's best friend is documentation.

4:30
Not even the best programmers memorize it all.

4:32
That's what the documentation is there for.

4:35
Before we head to the next video, let's test our knowledge so far with a quiz.

4:40
Remember it's perfectly fine to write the code out in the playground

4:43
to evaluate the results before selecting your answers.
You need to sign up for Treehouse in order to download course files.
Sign up