Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Start a free Basic trial
to watch this video
When using multiple operators in one expression the order in which they are executed affects the resulting value.
Further Reading

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

0:05
to use basic math operations.

0:07
But what happens when you want to go beyond basic math?

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

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

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

0:23
which you need to apply complex math.

0:26
If you understand this stuff now, you will thank me later.

0:30
So, back in Xcode, we're going to open a brand new playground.

0:34
So go to File > New > Playground.

0:38
And we're going to call this playground Operator Precedence.

0:41
Because that's exactly what we're going to explore right now.

0:49
Okay, so once again let's delete this var str.

0:52
And 'm going to create variable x and

0:58
assign it a whole series of calculations, so

1:02
I'm just doing something random here so just bear with me.

1:07
Now, I have practically all the operators here,

1:11
I have addition, multiplication, subtraction, and remainder.

1:18
So, in the results pane you can see that the value is 197.

1:23
So, how did the system arrive at this conclusion?

1:28
If we were calculating from left to right you

1:31
would see that we would get a different value.

1:34
So let's do that.

1:35
Let's do 100 plus 100.

1:38
That gives us 200.

1:38
So, if we're going left to right, then we would do 200 minus 5, which is 195.

1:46
And then 195 times 2, that gives us 390, right?

1:53
Let's keep doing.

1:54
So 390 divided by 3.

1:59
That's 130.

2:00
And then 130, remainder 7 gives us 4.

2:05
But as you can see here, that the value is 197.

2:10
And since we calculated from left to right, it gives us 4.

2:16
So, what's going on here.

2:20
Some operators take higher precedence than others.

2:23
When the system looks at a line of code like ours, it knows which operations to

2:28
perform first, and which operations to perform next.

2:33
So multiplication, division, remainder, addition and subtraction.

2:38
Swift assigns a priority, or precedence level to these operators.

2:44
Precedence level 150 is given to multiplication, division, and remainder.

2:50
Whereas precedence level 140,

2:52
which is lower, is given to addition and subtraction.

2:57
So, how does the system know which operators to execute when they

3:01
have the same priority.

3:03
It's actually quite simple.

3:05
All these operators work from left to right.

3:09
Let's try grouping them with parentheses so

3:11
we can understand how this is calculated.

3:14
Yes, that's right, you can always use parentheses if you simply want to

3:17
understand the order of operations for yourself or for others.

3:23
So let's find out how.

3:24
So, since multiplication, division and

3:27
addition have the same priority, which is higher than addition and subtraction.

3:33
And it starts from left to right.

3:35
That means that 5 times 2, because that's the first operation that's

3:41
higher in precedence, is going to be executed.

3:44
So that's 5 times 2, and then, if we're going left to right,

3:50
there aren't any other higher precedence on the left of 5 times 2.

3:54
So we have to move right.

3:57
Then, after it multiplies 5 times 2, it's going to divide that number by 3.

4:04
And then finally, it's going to

4:09
divide that resulting value, or get the remainder of that resulting value by 7.

4:15
And then it will perform the rest of the addition and subtraction.

4:20
So, let's see if this actually works.

4:23
We'll do 5 times 2, and we get 10.

4:27
And then 10 divided by 3, we get 3.

4:34
Then you have 3 remainder 7.

4:39
Which is also equal to 3.

4:40
And then finally you have 200 minus 3, which gives you 197.

4:48
So there you go, now we understand how the system arrived at that number 197.

4:57
I know you might be feeling a bit overwhelmed right now,

5:00
because it is a lot of information.

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

5:06
There are reference links in the notes section which you can bookmark and

5:09
refer to them any time you want.

5:12
A programmer's best friend is documentation.

5:15
Not even the best programmers memorize it all.

5:18
That's what documentation

5:26
is for, you can go back

5:32
to it anytime you want.

5:39
[BLANK_AUDIO]

5:41
[BLANK_AUDIO]

5:43
[BLANK_AUDIO]

5:44
[BLANK_AUDIO]

7:29
[BLANK_AUDIO]

7:31
[BLANK_AUDIO]

8:37
[BLANK_AUDIO]

9:11
[BLANK_AUDIO]

9:12
[BLANK_AUDIO]

9:14
[BLANK_AUDIO]

10:54
[BLANK_AUDIO]

10:55
[BLANK_AUDIO]

12:02
[BLANK_AUDIO]

12:35
[BLANK_AUDIO]

13:09
[BLANK_AUDIO]

13:43
[BLANK_AUDIO]

14:17
[BLANK_AUDIO]

15:20
[BLANK_AUDIO]

15:22
[BLANK_AUDIO]
You need to sign up for Treehouse in order to download course files.
Sign up