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
Learn the syntax for performing basic arithmetic operations and how computers decide which compute first.

0:00
In our last video we introduced you to the concept of an operator, and

0:04
quickly showed you the syntax associated with certain common operations.

0:08
In this video, we'll go in depth on the arithmetical operators.

0:11
That is, how we perform addition, subtraction, multiplication, and division.

0:18
Let's create a new command line application called mathOps.

0:21
We'll use it to practice our operators.

0:23
Again, we use a command line application, because we wanna focus on the language.

0:27
And not worry about any user interface.

0:29
You'll be learning about interface builder in a later course.

0:32
Okay, so let's give ourselves some space in our main function.

0:38
Then let's declare an int variable called apples, and set its value to 3 + 5.

0:45
Now if we run this code, it will compile and run just fine.

0:48
But we won't see the resulting value for apples.

0:52
Up until this point we've been creating logs to print values,

0:55
like the NSLog we see right here.

0:59
However, instead of doing that we will insert what is called a breakpoint.

1:04
And use that as a stopping point where we can check the values of our variables.

1:08
If you don't know, a breakpoint is pretty much what it sounds like.

1:11
It's a marker we put in code which, if active,

1:14
will temporarily stop the code from running.

1:16
To create a breakpoint in XCode, you simply click on the left side,

1:20
here where the line numbers are, and a blue marker appears.

1:24
The fact that the marker is blue means the breakpoint is active.

1:28
If I click on the breakpoint it grays out and would be inactive.

1:32
I can reactivate it by clicking again.

1:34
I can even slide it up or down in the code.

1:37
And if I wanna remove it, I simply drag it, release it, and

1:41
it disappears in a puff of magic.

1:43
I love that animation, it just never gets old.

1:46
Okay, so let's put our breakpoint here.

1:49
We're gonna use command R to run this time.

1:54
And we see that our breakpoint has stopped.

1:56
It stops here on the next line, after our breakpoint.

2:00
But when it does, you notice down here, apples equals 8.

2:05
I can even mouse over, and it again shows 8.

2:08
As you can imagine, when you need to inspect tons of variables.

2:12
Writing logs for all of them would be very time consuming.

2:15
So this is a nice option to have.

2:17
Now we generally use breakpoints to debug larger problems, not just for

2:20
checking a single value.

2:22
But as you'll learn,

2:23
very often that larger problem will boil down to some simple value or single line.

2:28
Now, in all that talk of breakpoints, you probably didn't even

2:31
realize that you just learned the syntax for addition in ObjectiveC.

2:34
Yep, it's just a plus sign.

2:37
And in case you're wondering, yes,

2:38
we can add more than two numbers together in a row.

2:41
Let's change this to 3 + 5 + 4 + 1, and we can run it again.

2:52
Now we see apples equal 13, just as we expect.

2:57
All right, who would like to come to the board and

2:59
show us how to perform subtraction in ObjectiveC?

3:02
Okay, no volunteers, I guess I'll do it myself.

3:07
int oranges = 103; let's run that again.

3:20
All right, down here we see that oranges = 7, 103, looks pretty good to me.

3:28
We could even create a new variable.

3:31
And let's call that totalFruit,

3:36
and have that equal apples + oranges.

3:42
And run again, we'll use control r.

3:45
Now we see totalFruit equals 20, just as it should.

3:49
Now let's try some multiplication.

3:52
We'll create an int called eggsPerCarton.

4:00
And we'll set that equal to 12, because there are a dozen eggs in each carton.

4:04
Then we'll create an int for eggs.

4:07
And we're gonna set that equal to eggsPerCarton x 4

4:11
because we'll assume we have four cartons of x.

4:15
We run it, and lo and behold, we see that we have 48 eggs, just as we would expect.

4:22
And just to be clear, it's the star, or

4:24
asterisk, that's acting as our multiplication operator.

4:28
Okay, now let's try some division.

4:32
For that, let's divide all the food items we have into baskets

4:36
with an equal number of items in each basket.

4:39
So first let's create a new int variable for baskets.

4:43
And this is the number of baskets.

4:47
We'll say we have 4 baskets.

4:49
Then, let's figure out how many items will go into each basket.

4:53
Now since our total number may not be evenly divisible by four,

4:56
let's create a float variable instead of an int.

4:59
Remember, ints can only hold whole numbers, no fractions.

5:03
So, we declare a float, we'll be sure to get our fractions.

5:06
So let's declare ourselves a float.

5:08
And we'll call it itemsPerBasket.

5:13
Then, we can actually just

5:18
set that equal to apples +

5:22
oranges + eggs / baskets.

5:28
Okay, let's run it again.

5:31
Okay, let's give ourselves a little more room in the console here.

5:34
So it looks like we've got 13 apples, 7 oranges, 48 eggs.

5:40
So that should equal 68 items.

5:43
We've got 4 baskets, but we're showing 32 items per basket.

5:50
Hm. 68 divided by 4 is 17,

5:51
it's definitely not 32.

5:53
Can anyone figure out what's going wrong, here?

5:56
I'll give you a hint, it's in this line, right here.

6:00
Anything suspicious?

6:01
Well, if you remember back to your Prealgebra class,

6:04
there's a little something called the order of operations.

6:07
In programming, we call the same thing precedence.

6:10
Essentially, it's a short list of rules that dictate which operations we perform

6:15
first, which come next.

6:17
And which might come after that.

6:19
When I was growing up, we used a little mnemonic device to remember the order.

6:23
We called it PEMDAS, which was an acronym for Please Excuse My Dear Aunt Sally.

6:29
The first letter of each word signifies an operation in the order of operations.

6:33
Those with the highest priority came first.

6:37
First we handle anything inside parentheses.

6:40
Then any exponents.

6:42
Then multiplication or division, whichever is further to the left.

6:47
Than addition or subtraction.

6:49
Again, whichever is further left.

6:52
So, getting back to our problem of putting food in baskets,

6:55
we can see what our issue is.

6:57
When the computer looks at the right side of the expression,

7:00
it first divides the number of eggs by the number of baskets.

7:04
That's 48 divided by 4 which gives us 12.

7:08
Then it adds 12+13+7, which is how we get 32.

7:14
Now take a few seconds and think about how we can quickly solve this issue.

7:18
Well, since we know that the operations inside parenthesis will be executed first,

7:24
let's put all the food Inside parenthesis.

7:29
Let's run it again.

7:33
Okay, now we see the items per basket is 17.

7:38
We added apples + oranges + eggs, and

7:42
then we divided the whole thing by the number of baskets.

7:47
Hopefully what we just learned should all ready be somewhat familiar to you.

7:50
But just to be sure, take a few minutes to work through the following questions.

7:54
After that, we'll move on to the other common ObjectiveC operators.
You need to sign up for Treehouse in order to download course files.
Sign up