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.
Further Reading

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

0:05
how to use the basic arithmetic operations, but

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

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

0:15
Now you're wondering what any of this has to do with building an app.

0:19
Well, when we build an app, there's usually animation in there.

0:22
And for animation, we need to apply math functions.

0:25
So this knowledge may come in handy then.

0:29
As always, we'll start with a comment.

0:31
So we'll say, Operator Precedence and

0:35
let's write out a fairly complicated arithmetic operation.

0:40
So we'll do var x = 100 + 100 5 * 2/3 and

0:49
then using the remainder operator,

0:55
there's 7 in there.

0:58
Now in the results pane, you can see the results of this expression as 197.

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

1:09
So first, we have 100 + 100, which is 200.

1:14
2005 is 195.

1:16
195 * 2 is 390.

1:22
390/3 is 130 and then dividing

1:27
130 by 7 gives us a remainder of 4.

1:32
Now this is obviously not the same as the result in the playground, so

1:35
what's going on here?

1:38
Well, you should know that some operators take a higher precedence than others.

1:43
When the system looks at a line of code as ours,

1:46
it know which operations to perform first and which to perform second.

1:52
It does not go from left to right like we just did.

1:55
Let's look at the operators that we used in this expression.

1:59
So we have the multiplication, division, remainder, addition and subtraction.

2:05
SWF assigns a priority or precedence to each of these operators.

2:11
Multiplication, division and remainder have the same priority level,

2:16
which is 150, whereas addition and subtraction have a priority level of 140.

2:22
So how does the system know which operators to execute when they have

2:26
the same priority?

2:28
It's actually quite simple.

2:29
Given the same priority, all these operators work from left to right.

2:33
So let's go back to our code and we'll try grouping them with parenthesis,

2:39
so that we can understand how this is calculated.

2:43
Now in your code, you can always use parentheses as well if you simply want to

2:48
understand the order of operations for yourself or for others.

2:52
Now using the parentheses and working our way from left to right,

2:57
let's first carry out the operations for any operator that has a precedence of 150.

3:02
So multiplication has a precedence of 150 and

3:07
it comes first in the order, so we'll multiply 5 by 2 and we get 10.

3:13
Now division has the same precedence level, so we'll divide 10 by 3.

3:18
Now you might be confused here, but we're using all integers, 10 divided

3:23
three is really 3.333, but SWF cuts it off, because this is an integer type.

3:29
This is a good example of choosing the types carefully is very important when

3:34
working with numbers.

3:35
Now the remainder operator also has the same precedence level, so

3:39
we'll use seven to get the remainder.

3:43
And again, it's three.

3:44
What is left then is again,

3:46
executed from left to right using the next precedence level, 140.

3:50
So we add 100 to 100 to get 200 and

3:56
then we subtract three to get 197.

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

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

4:09
Operator precedence is definitely not something you need to know off the top of

4:13
your head and very rarely is ever used in practice even if you did need to

4:17
know them, there are reference links in the note section you can bookmark, so

4:21
you can refer to them any time you want.

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

4:28
Not even the best programmers memorize it all, that's what documentation is for.

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

4:38
Remember, it's perfectly fine to write the code out in a playground

4:41
to evaluate the results before you select your answer.
You need to sign up for Treehouse in order to download course files.
Sign up