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
Every programming language allows you to perform basic math like addition, subtraction, division and multiplication. These are called binary operators.
Further Reading

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

0:10
subtraction, division and multiplication.

0:13
These are called, binary operators.

0:16
They called binary operators, because they have numbers on either side.

0:21
These numbers are called operands and

0:24
the symbol in the middle is called the operator.

0:28
Let's list out all the operators in Swift.

0:31
[SOUND] You have addition, [SOUND] subtraction, [SOUND] division,

0:36
[SOUND] multiplication and remainder.

0:39
[SOUND] The last one,

0:40
might be a bit misleading because the operator is a percentage sign.

0:45
However, it doesn't give you the percentage, it gives you the remainder,

0:50
when dividing two numbers.

0:52
In the above example, 11 divided by five gives us a remainder of one.

0:58
Or another way of seeing it, is that we can fit two fives, with an 11 and

1:04
one remains, so let's take a look at how we can use these operators in our code.

1:12
So, going into Xcode, we'll go up to the file menu, select New playground.

1:19
We'll call this playground operators, leave platform as iOS,

1:25
hit Next and then hit Create.

1:32
All right, once again deleting the var STR [SOUND] because we don't need that.

1:36
So the first thing we'll look at, is the edition.

1:40
Now, if you've ever done any algebra, you've probably used x.

1:45
You know, you've always, like, had to find out what is x.

1:49
So let's say var x = 1 + 2.

1:54
And in the results pane you will see that the value is 3.

1:59
Now of course since it's a variable we can name it anything we want,

2:04
we don't have to call it an x.

2:07
So, let's take a real world example, so I'm gonna delete this line.

2:12
And let's say we have the height [SOUND] and width of a room.

2:15
So I'll say height [SOUND] is equal to 12 [SOUND] and width is equal to ten.

2:24
So, I'm assuming that this height and width are in feet.

2:29
[SOUND] Now, you're also probably wondering why these are constants.

2:36
Well they're constants because the height and width of a room don't exactly change.

2:41
So, let's figure out the area of that room.

2:45
So I'll say, let area equal, so we have to multiply the height times width.

2:52
[SOUND] So I'll say height,

2:54
[SOUND] the multiplication sign is an asterisk [SOUND], times width.

3:00
[SOUND] And, this will give us the area of the room.

3:04
Of course, the area will be in feet.

3:08
Once again I've used the key word let, which creates a constant.

3:12
And, all of these are constants because we're assuming the height and

3:15
width of a room don't change, well, unless you knock down a wall or something.

3:20
But, we're not gonna get into that mess.

3:23
So let's say we want to convert the area into square meters because, most of

3:28
the world uses the metric system, except the United States, because we're special.

3:33
So we're gonna say, let area in meters [SOUND] equal area divided by something.

3:42
So that's the division sign, this forward slash over here.

3:46
So how do we find out what is the area in square meters?

3:49
So one square [SOUND], meter is equal

3:55
to one square foot divided by 10.764.

4:03
So I already have my area in feet.

4:06
So all I have to do is divide this by 10.764.

4:11
[SOUND] And this will give me the area in meters.

4:15
Now you'll notice that in the results pane on the right, it gives me a number which

4:22
is a whole number, so this is exactly 12 meters or 12 square meters rather.

4:29
That can't be right because when you divide a number by a fraction,

4:33
you should get a number with some decimal places.

4:37
So something is going on here.

4:39
Swift is making some assumptions on our part.

4:42
And it's assuming that, if our area, the number that we're dividing by,

4:47
is an integer, then the results,

4:51
which is this area in meters, will also be an integer.

4:56
And we don't want it to be an integer.

4:59
So one way of converting this, is, we can go back here and

5:05
make sure that both our height and width are doubles.

5:09
So all we have to do is put a .0 on to this, .0.

5:14
So now you'll see that the number on the results pane is a big decimal number.

5:21
That's one way of doing it.

5:23
But what if we are writing a program and

5:26
we didn't have the ability to change the original constants?

5:30
We didn't, we couldn't change the fact that the height and width were doubles.

5:36
So we can go here.

5:38
Let's, let's just go back and make these integers.

5:42
So there's a way that we can convert our existing number in to a double.

5:48
So we can convert this area in to a double.

5:52
So just like we used the print function and we passed it.

5:55
A variable, we can use double,

5:59
[SOUND] the type double [SOUND] and pass it a variable.

6:05
And what it'll do is, it'll convert that variable or

6:09
that constant from that particular type into a double.

6:14
Granted of course that, you know that has to be a float or

6:17
an integer you can't pass it a string and expect it, to create a double unless it

6:22
is a valid number but if you had like a string flow world,

6:27
it would make no sense to convert that into a double.

6:31
And that's one way we can convert a constant into the type that we want.

6:38
So now area in meters is a type double and it has decimal places and,

6:44
this would matter for accuracy, cuz if you're writing a program that requires you

6:48
to calculate stuff in you know, have all these different calculations,

6:53
you have to make sure that you provide accuracy to your user.

6:57
We've looked at multiplication, addition, and division.

7:02
Let's look at the remainder function, or the, the remainder operator.

7:08
So let's assume that we have chairs.

7:12
So let me create a constant.

7:14
I'll call this chair width.

7:15
[SOUND] And I'll give it, let's say it's three feet, right?

7:21
So we have chairs and we wanna line,

7:23
we wanna see how many chairs we can line up against a wall, or against the wall

7:28
that we have already which is the width of this wall is ten feet.

7:34
All right. So,

7:35
how many chairs can I safely line up against that wall?

7:38
I mean, if you just did basic calculation you would see that you

7:42
can fit three chairs.

7:43
But, let's say we need to figure this out programatically.

7:47
So I'll create another constant called space remaining [SOUND] and

7:52
what I can do is, I can take the width and

7:56
use the remainder operator and provide it with the chair width.

8:01
So essentially I'm taking the width of the wall using the remainder operator, and

8:06
then using the chair width.

8:08
And, what this will give me is how much space is

8:12
remaining in the wall after I have accommodated, x amount of chairs.

8:19
So, to calculate the number of chairs [SOUND] I

8:22
can do width divided by chair width.

8:25
[SOUND] So this will give me the number of chairs I can fit on that wall,

8:30
and the space is remaining for

8:33
other decoration on that wall, whatever you wanna put.

8:37
This is of course an example that I've created, but

8:40
the remainder operator actually has a lot of used cases which you will find.

8:45
You know, for example, you can always divide a number by two and

8:49
if the remainder is zero you know it's an even number.

8:52
So that's one way of calculating even numbers, or calculating odd numbers, for

8:56
that matter.

8:57
So there you have it.

8:58
Here are all the basic operators that you'll be using when,

9:02
you know, performing all kinds of math calculations.

9:05
Now, one thing happens is that when you have multiple operators on the same line.

9:10
There's a precedence on how these operators get executed.

9:14
And that's exactly what we'll cover in our next video.
You need to sign up for Treehouse in order to download course files.
Sign up