So today we are going to talk about arithmetic operators, so we've gone over the logical operators.
Now it's time to talk about some of the operators that have a little bit more to do with standard math,
things like addition multiplication division, stuff like that.
And we'll notice there's actually some, some more operators besides just those standard ones that I've
just mentioned.
So I went ahead and took the liberty of making a new file here called arithmetic operators to keep you
notice.
I'm quite lazy and I'm still putting it in this first program directory.
But of course, it's a good idea for you to organize your files in a way that is easy for you to go
back and look up things for reference.
So I just put it in here, but you can make it wherever you want.
I also went ahead and pulled up another terminal.
I'm doing it in Visual Studio, so you can see again today.
So you can just pull the terminal up as well so we can go ahead and compile and run our stuff.
I have already made a main function in the skeleton with some comments, and you might be wondering,
Hmm, I haven't seen this before.
Up here.
What's with this?
So I'm just going to kind of sprinkle in some random things to the lectures that aren't having their
own dedicated topics.
You noticed that I already mentioned something about comments you might have seen that mentioned in
other videos as well, but we talked about a comment on a single line, which is a slash slash before
it.
This is the same thing, except it's a multiple line comment.
So it's kind of nice sometimes to put a description for your entire program at the top.
Or if you needed to describe another big chunk of code, you might want to put a block comment, which
is something else.
You can call this or a multi-line comment.
The way to do that is to put a slash and then a star to open the comment.
Then you can type as many lines as you'd like and you close it with a star and then a slash so you could
add more lines if you hear if you wanted to another line.
You know this if I get rid of this, you notice it just goes all the way down to here.
It sees this star slash that they put right here, and it considers these comments just because they
have slash slash is already in front of them.
So you got to make sure to close it.
But yeah, just something else I wanted to mention.
So you don't have to have these on a new line, either.
I can back this up right on the same line.
I can also work this out right here.
You can put it like that.
However, you would like it to look as long as there is an open slash star and a closed star slash.
OK, so pretty cool.
You know, I'm also including, I assume, because of course, we're going to be using that as always
to print out stuff today.
I'm going to go ahead and not do the using namespace skinny thing.
I'm actually going to use the study for the standard namespace in the colon colon scope resolution operator
to put before see out and before in line.
Just because I'm going to mix it up, we'll be using it sometimes or sometimes not be using it.
Like I said, I don't think it's like a horrible thing to use it, like you should never use it ever.
You know, it's put there for a reason, it's possible to do it for a reason, so, you know, maybe
not a good reason, but you can use it if you don't have to worry about some near-space collisions,
that's something we'll get into later.
But it's good programming practice to not use it and instead do this, which you just have to put this
before anything that you would use inside of extreme.
Something extreme is coming from the standard template library and C++, so it gives us access to things
like see out and in.
And so the difference is is you're going to see this.
Rather than just this, and you will see this rather than just this.
That's the only difference today, at least.
OK, so with that, let's go ahead and get started.
Make sure you have a main function here with the brackets.
And then I have a lot of comments in here that just talk about what we're going to get into and discuss.
So first off list, declare some variables.
I'm going to leave this up to you.
So why don't you pause the video and come up with an integer of float and a double variable?
So hopefully you went ahead did that.
I want you to get some practice and remember how to do that because we have discussed that already.
So if you're still a little wobbly, you can go ahead and just do something like this.
But whatever value you want, I'm just going to say this is five an integer called i1 that has the value.
Five.
I'm going to say a float and I'll call this f one equals ten point five, and I'm going to say a double
b one.
And I want to say this is fifteen point twenty five.
Cool.
So now we have some variables.
So the first thing that we're going to go over is the basic operators.
So these are things that you've probably seen before in math, this asterisk here is for multiplication.
This plus is for addition, the minus is for subtraction.
The slash is for division.
And this you may or may not have seen you.
Some of you might know this as the percentage sign, but it also has in programming.
We refer to it as the modulo or modulus.
So this just means remainder, and you might have already used modulo or modulus in mathematics.
If you've taken a math class that deals with that stuff, maybe like discrete math or a number theory
or something like that.
But if you haven't, don't worry, I will explain what its purpose is and how to use it.
So with that, let's go ahead and print out some mathematical things, like some operations.
So I'm going to do ask Colin and call Remember and then ask out and I'm going to do.
Let's just do something simple, so I'm going to do I one.
That's our variable that has the integer five.
And I'm going to do.
Plus I won.
Actually, I do have Class One to call, and then here I'm going to do an SUV and in line.
So let's go ahead and print this out before we do that.
Tell me what you think this result is going to be.
Of course, I can't hear you say that, but just think about what it would be on your own before it
gets printed out.
So I'm going to save this and I'm going to actually meet all the D Plus plus I, so I'm in the directory.
If you need a change to your directory, go ahead and change to the directory that has your file in
it.
That would just be.
Make sure to follow this path up here.
So I'm going to do G-force plus arithmetic operations to repeat that, oh, to name this swimmer, to
name my alpha file and I'm going to call this just areas that you see.
OK, now I can run it, so I'm going to do that slash Anthony ACSI.
And we see in 11 here, so what happened was we had a I one was five plus one is five, so that's 10.
And then we added one to that, which gave us 11.
So that's why 11 printed out.
So it actually printed out the result of all this.
Cool.
So let's try something else.
We'll do another line this time we'll do some multiplication, so why don't we do F1 times to
put that SUV phone call in?
And so, yeah, let's go ahead and save this and run it.
So I'm going to compile this and run it.
And we noticed that we get twenty one, so this is a ten point five times too.
So, you know, ten times to twenty point five times two is one.
We have twenty one here for our result.
Let's go ahead and do some divisions.
So this time I'm actually going to throw in some
parentheses just for the heck of it.
So remember, the parentheses are things that you can actually use to separate a lot of stuff.
So in math, you might have used like parentheses when you talked about the order of operations, which
is what we will do next.
We'll talk about the.
Operator precedence, which is order of operations, but you and Matthew can sometimes separate things
with parentheses, right?
If you want something to happen first before something else happens.
So I'm going to do that just right now, kind of.
It's a little like precursor to us going over the order of operations.
So I'll do when I want times to.
So that is going to be five times too, right?
Because five store in here.
So that'll give us 10.
And then here I'm just going to say divided by five.
So that'll happen afterwards.
And you don't have to put spaces here if you want.
You can just go like this and put them right up against each other, I like to put spaces just to make
them look nice, but you know, I'll go ahead and leave that with no spaces just to show you.
So I went ahead and saved it.
I'm going to go ahead and compile it and run it.
So we didn't see we get to here.
So what happened was we did five times two, which was 10, and then we did 10, divided by five, which
was two.
Cool, so pretty interesting.
You know, we can actually, like, add some stuff in here like I could do a minus one and compile this
and run it, and then that becomes a one because it subtracts the ones that's using the subtraction
operator.
Let's look at this interesting one, though.
The modulus.
So I'm going to do
another.
Let's see.
I just choose.
I want.
Actually, I want is five.
So I will do I one one two.
So what this is going to do is it's actually going to get the remainder of the division by two.
So when I say division by two, I'm talking about five divided by two because I want to has five, right?
So five divided by two.
Two only goes into five two times.
And there is a remainder of one left right, because two times two is four.
That's that's one less than five, there's a remainder of one.
So let's let me kind of be more concrete about this, too.
This has there's a little bit of a nuance with the division, too, so I should point that out first.
Actually, the division is depending on what you have over here.
So if I do, I one divided by two.
Let's see what prints out, actually.
So I'm going to go ahead and say this and run this.
OK.
So you notice that only two prints out, but five divided by two is not to what it's doing is it's actually
just rounding it down.
So it's just saying OK to the integer two goes into five two times and it gives you an integer result.
But what if I was to go like this and put it two point o instead?
So now it's not an integer type that I'm dividing it by looks.
It is a floating point, kind of, you know, like this float double.
So let's go ahead and save this, and then I go down here, compile and run it.
So you notice now it says two point five, which is actually accurate.
The more accurate, you know, five divided by two is actually two point five.
So if you add a floating point here on the right side of the operator, so you have a floating point
upper end that will give you a floating point result here.
But if I just have the two, then it just kind of rounds it to two, even though there's that one left
over right.
We talked about the remainder of one.
So what happens if I put the modulus, then let's go ahead and run it.
So it actually says one.
OK.
And it does that because there is one left over like we talked about two times.
You know, it goes in two times and two times two is four and five minutes for one, there's one left
over from that modulus.
There's something really interesting.
It's actually used a lot in computer science, as we'll see later in the course.
It can have much broader applications, so really cool, operate a really cool concept.
All right, so that's kind of all I have to say about these basic operators.
Let's go ahead and move on to like using more parentheses and talking about the precedence between operators.
So I'm actually going to comment this out for now.
So what we can do when I say commenting out, we're just going to transform these lines into a comment
by putting the flags in front of it.
So.
Like this list now, they're considered comments, so this code is not going to run anymore because
it has to slash last before it's seen as just some comment, just like this one.
So you can uncomment them if you want to run them, but I'm going to count them out just so we can have
kind of a clear, concise output here.
So let's go ahead and use our variables again.
But what we're going to do now is discuss the order of operations or operator precedent.
So we have this multiplication is the first.
Then after that comes division and then we have the modulo and then we have plus and then we have minus
here.
So that's kind of the order of operations, if you have heard of that in math and some other math class,
you know that there was an order to things like you maybe have heard of him dos, which was like starting
with the parents parentheses, the parentheses, exponents, multiplication division, addition, subtraction.
That's what that acronym stood for.
So in code, we just call it operator precedence, and it's really the same thing.
So if I knew another line here, I'm just going to copy this
cool.
So let me go ahead and.
Modify this a little bit, so let's think about and think about this, so if I want to do something
like I'm one plus two times three minus one.
So let's go ahead and save this and compile it and print it out.
So we have 10 here, so I want us five, right?
So we have five plus two times, three minus one.
Well, you know, what's actually happening is this first, so we are two times three, which is six.
And then what it's doing is it's saying, Oh, OK, we do this first three times two six, six plus
five is what it'll do because of the order, other operations, which is 11.
And so this is 11.
And then it does a minus one, which is 10.
So what if we didn't want it?
What if we wanted to just have it in left or right or, well, then we'd have to use parentheses, right?
So sometimes it's good to use parentheses if you want to force some some sort of mathematical ordering.
So right now, we expect it to do five plus two.
So there'd be five, six seven, we'll have seven times three then, which will happen next because
multiplication happens before subtraction, seven times three is twenty one.
And then we'll have a minus one.
So we should we should get the result of 20.
So I say this, compile it, run it and we get a result of 20 like expected.
So that's kind of just, you know, what's going on with precedence here.
If you want to, you know, if you wanted to flip this around, let's say we did a minus three times,
maybe like two or something like that.
You know, what it's going to do is it actually is going to evaluate three times to first and then it's
just going to subtract that from this.
So if you want this to happen all in the same order, really what you're going to need to do like in
left to right, what are you going to need this surround this in parentheses as well?
So that is something that you can use as these parentheses to force the ordering, otherwise that happens
in this order here.