This course will be retired on May 31, 2020.
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.
Links

0:00
[MUSIC]

0:04
Every programming language allows you to perform basic math like addition,

0:08
subtraction, division and multiplication.

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

0:16
An operator is a special symbol or

0:18
phrase that you used to check, change or combine values.

0:23
Operators can be separated into three categories.

0:27
Unary operators.

0:28
These operate on a single target.

0:31
We also have binary operators, ones that operate on two targets and

0:35
ternary operators, those with three targets.

0:39
We'll start with binary operators because they're ones we're most familiar with.

0:43
We've already used an operator or two in this course.

0:47
Let's look at one that I'm sure we all know.

0:49
This is the addition operator.

0:52
It is a binary operator,

0:53
because as we defined earlier, binary operators operate on two targets.

0:59
In this case, our two targets are numbers that we add together.

1:03
The values that operators affect are called operands.

1:07
So in our case, the two numbers are called operands,

1:11
a combination of an operator with operands is called an expression.

1:16
So this line of code that we see here is an expression.

1:20
There are many binary operators most of which you are familiar with

1:24
from basic math.

1:26
The one we just saw, the addition operator, where we also have

1:29
a subtraction operator, division, multiplication and a remainder operator.

1:35
When I was in school I had to learn long division.

1:38
You remember that?

1:39
Feels like forever ago.

1:41
In long division, if you divided 10 by 3,

1:43
3 would fit into 10 three times and you would get a remainder of 1.

1:49
That is what the remainder operator does, it tells us given a number and

1:54
its divisor what is the remainder that is left over.

1:58
It is sometimes also known as the modulo operator.

2:02
While the point of this operator may seem silly, it's actually quite useful.

2:07
If we took a number and a divisor 2 and use the remainder operator.

2:12
If the remainder is 0, well, now we know that this is an even number.

2:17
Those are the main binary operators we'll look at for now, they're pretty simple.

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

2:26
is that these operators we looked at are called infix operators.

2:31
An infix operator is one that goes in between two operands.

2:36
All binary operators are infix operators.

2:39
So if you're confused about the syntax when writing it out,

2:42
remember that they go in between the values they're working on.

2:46
Let's start by playing around with binary operators for a bit.

2:50
We'll add another page to our playground to talk about operators first.

2:54
So like before click on the first icon in the second set here

2:59
to open up the project navigator.

3:01
And then using the plus button below, the Add button, let's add a new page.

3:07
We're going to call this one operators.

3:11
Let's add a comment in here.

3:13
We're going to talk about binary operators first.

3:17
Okay, so let's say we have the height and width of a room in feet.

3:22
Yeah, yeah, I know not everyone lives in the US and uses feet.

3:26
Don't worry, we'll work around that.

3:28
So let's create a new constant height and we'll assign it an integer value.

3:33
So say 12 and I'll add a comment, we'll say in feet.

3:37
We also need the width for this example.

3:39
So we'll say let to create a constant, width for

3:42
the name and we'll assign another integer value.

3:46
Now remember I can also explicitly write out the type like so

3:51
and we'll add another comment in saying in feet.

3:59
Now to calculate the area of the room which is a rectangle,

4:03
we can simply multiply the height and the width.

4:06
Let's do this and assign this to a constant called area.

4:09
We'll assign this new value.

4:10
So we'll say let area equal height times width.

4:17
So this is the multiplication operator, which is the asterisk symbol.

4:22
And it's a binary operator which goes in between two operands like so.

4:27
Now the unit of measurement for this is area in square feet,

4:32
now like I said we don't all use square feet.

4:36
So let's try and convert this into square meters.

4:39
Let's add another comment here.

4:41
So we'll say one square meter equal

4:47
one square foot divided by 10.764.

4:54
Okay, so to get this area in square feet

4:58
into meters square, we can create a new constant.

5:02
We'll call this areaInMeters and

5:07
we'll simply divide the area by 10.764.

5:13
Okay, when you try this, you'll see that we get an error and

5:18
this is because of a conflict between the types.

5:22
Now I don't have an error symbol over here but

5:24
you can see in my console that there is a bunch of stuff going on.

5:29
And you can bring up your console by clicking on the second icon in this set

5:33
of three, the second set.

5:35
Okay, so what's going on here?

5:37
The height and width values are of type int since they're whole numbers.

5:42
And since we're multiplying them to get a value for

5:44
area, Swift infers that the area should also be of type int.

5:51
Now we can't simply combine two types willynilly.

5:55
In real life you could.

5:56
You can simply divide this area which would be 120 by 10.764.

6:00
We know what to do, but the compiler doesn't.

6:04
We cannot divide an int by a value of type double

6:07
because Swift doesn't know which type the answer should be.

6:11
Should the value be an integer value?

6:13
In which case, Swift will go ahead and round off the answer and

6:16
cut off the values after the decimal point.

6:18
Or should it be a double.

6:20
Now we can solve this for Swift by changing the values for height and width

6:25
to be double by either adding decimal points or explicitly specifying the value.

6:30
So we'll say let height double, let width double.

6:35
And now everything should resolve itself.

6:37
So pretty simple stuff as you just saw, we use the multiplication and

6:42
division operators.

6:43
But it's important to know that unlike in the real world,

6:46
you have to be careful about what types you use.

6:49
You can't simply divide an integer by a double or even multiply it.

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