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
We'll start working with operators that we're most familiar with  addition, subtraction and other basic mathy stuff.
Further Reading

0:00
[MUSIC].

0:05
You know how when you're going to start learning how to code and you think,

0:08
this is going to be about math, and I don't like math, but you know,

0:11
programmers tell you programming isn't about math.

0:15
Well, programming isn't about math, except for

0:17
the next set of videos that we're going to learn.

0:20
Every programming language allows you to perform basic math.

0:23
Like addition, subtraction, division, and multiplication.

0:28
To achieve these tasks, we use operators.

0:32
An operator is a [SOUND] special symbol or

0:35
phrase that you use to check, change or combine values.

0:39
Operators can be separated into three categories.

0:43
Unary [SOUND] operators, which operate on a single target,

0:47
[SOUND] Binary operators, ones that operate on two targets and

0:52
[SOUND] Ternary operators, those with three targets.

0:56
We'll start with binary operators because they are the ones we are most

0:59
familiar with.

1:01
We've already used an operator or two in this course already.

1:05
Let's look at one that I am sure that we all know.

1:08
This is the addition operator.

1:10
It is a binary operator because as we defined earlier

1:15
binary operators operate on two targets.

1:18
In this case our two targets are numbers that we add together.

1:23
Values that the operators effect are called Operands [SOUND] so,

1:28
in our case the two numbers are called Operands.

1:31
A combination of an operator with Operands is called an Expression.

1:37
So, this line of code that we see here is an Expression.

1:41
There aren't many binary operators.

1:44
Most [SOUND] of which we are familiar with from basic math.

1:47
This one that we just saw is called the addition operator.

1:50
We also have a subtraction [SOUND] operator, division [SOUND],

1:54
multiplication [SOUND], and a [SOUND] remainder operator.

1:58
When I was in school [SOUND], I had to learn long division.

2:01
You remember that?

2:02
It feels like forever ago.

2:04
Now, in long division, you had a number that you wanted to divide, [SOUND] let's

2:08
say ten [SOUND], and a divisor, that is a number that you are dividing with [SOUND].

2:12
Let's say it's three.

2:14
In long division, if you divided ten by 3 [SOUND] 3 [SOUND] would fit

2:19
into ten three times, and you would get a [SOUND] remainder of one.

2:24
That is basically [SOUND] what the remainder operator does [SOUND].

2:27
It tells us given a [SOUND] number and

2:29
it' [SOUND] divisor what is the remainder that is left over.

2:33
It is sometimes also [SOUND] known as the Modulo Operator [SOUND].

2:37
While the point of this operator seems kind of silly it's actually quite

2:41
useful [SOUND].

2:42
If we took a [SOUND] number and the [SOUND] divisor two and

2:45
used the remainder [SOUND] operator if the remainder is zero,

2:48
we know [SOUND] that this number is an even number.

2:51
Those are the main binary operators we'll look at for now.

2:54
They're pretty simple.

2:57
Something that's interesting to know, but not at all necessary

3:00
Is that these operators we looked at are called infix operators.

3:04
An infix operator is one that goes in between the two operands.

3:09
All binary operators are infix operators, so

3:13
if you're confused about the syntax when writing it out,

3:15
remember that they go in between the values that they're working on.

3:18
Let' splay around with binary operators for a bit.

3:23
Now before we do that, let's go to the project navigator on the side [SOUND] and

3:30
we'll add a new playground page to talk about operators [SOUND].

3:36
Like before, I'm just going to get rid of everything in here and

3:40
we'll add a comment.

3:41
[SOUND] Let's say we have the height and width of the room in feet.

3:50
Yeah, I know.

3:51
Not everyone lives in America and uses feet.

3:53
But don't worry, we'll work around that.

3:56
So, first we'll start with the height [SOUND].

3:58
And we'll give it a value of 12.

4:02
Now this is of type int.

4:05
And I'll add a comment so that we can remember it's in feet [SOUND].

4:09
We'll also create a constant for the width with a value of ten [SOUND].

4:15
Also in feet [SOUND].

4:18
Now to calculate the area of the room,

4:21
which is a rectangle, we can multiply the height and width.

4:26
So, let's assign this to a constant called area.

4:30
So, we'll do let area equal.

4:34
We just saw the multiplication operator, which is the asterisk symbol.

4:39
Now, this is a binary operator, which goes in between operands,

4:44
so we'll write this as height times the width.

4:50
Now the unit of measurement for this is Area in sq.

4:55
feet.

4:57
Now like we said, we don't all use square feet so

5:01
let's try and convert this into square meters.

5:04
Now we'll add a comet because I don't know

5:09
this value off the top of my head, but

5:13
one sq meter is one sq foot divided by 10.764.

5:19
So to get this in square meters we can declare a new constant called

5:23
areaInSquareMeters and will assign the result of this expression.

5:31
So the area in square feet divided by ten point 764.

5:35
Now when we try this, you may get an error,

5:40
or if your playground's acting weird, you may get grayed out values like this.

5:45
And what's gong on here is, we actually have a conflict of types.

5:50
Now the height and the width values are type int, since they are whole numbers.

5:55
Since we multiply two integers to get the area value, that is also of type int.

6:02
Now when we tried to get the area in square meters,

6:05
we can't divide a value of type int by a value of type double,

6:09
because swift doesn't know which type the answer should be.

6:14
Should the area in square meters be in int?

6:17
If it is, or if it should be, then swift will round off the answer,

6:21
because it can't have decimal places, and we don't want that.

6:24
Or should it be double?

6:27
Now we can solve this for swift by changing the values of height and

6:33
width to double by adding a decimal point [SOUND].

6:37
Pretty simple stuff right so far?

6:39
So we've used both the multiplication and

6:42
division operator which were both binary operators.

6:47
In the next video, let's take a look at some other binary operators used in swift.
You need to sign up for Treehouse in order to download course files.
Sign up