In this lecture we'll go over medic operations that you can do these will really be like grade school
math that you've done.
So what starch won't say we have a variable x and we'll sign it five in one of it y and will sign it
11 Let's make another one say Zed and we'll say X plus Wise's the addition operator so we'll see what
this output it's
you said is
No it's right.
And it gives you 16 as you probably expect.
So it's so shy a minus.
He's negative 6 and multiply that should give you five.
Right.
So these are what are called a binary operator binary meaning there's two sides to this operators.
You have the operator in the middle and then you have on the left side of this X and then on the right
side the Y.
That's why it's binary because it works on two different sides of itself.
But let's see what happens if you say divide things get a bit more interesting.
It's from that and the value is zero.
Rants are saying 5 which is the value X and 11 which is the value y and 0.
And why is that while.
Like I said in The Last Lecture types are interpreted in a specific way and integers are whole numbers.
So you can't have something like zero point five or something like this for like that it'll always lop
off the end of it which should be its decimal point onwards.
And just use the whole number which would be zero in this case.
So you have to be very careful about that.
In fact you can actually take advantage of it in certain situations which we'll probably see later on.
So let's use a different operator called the modulus operator.
This is the percent sign.
So if you remember back in your grade for five or three math class you probably went over something
like long division and if you remember if something didn't evenly divide you always get a remainder.
And this is what this is.
So if you had say 11 as X into Y What do you think the value would be.
What is the Zenn value here.
Well it's one because you will divide 11 five times but there will be one remainer rate and that's what
that's what this one is the remainder of the division that was was done
and modulus is very useful for certain things and be using it quite a bit more later on in our programs.
Is also very nice shortcuts to some of the operations that we can do.
So say you wanted to we had X equals one and we said it's said as.
For now let's say we had wanted to say X equals x
plus 1 plus one can.
So we're saying X gets value x plus 1.
So at the end of this this should be two right.
And we should have four as our result for zed
for here.
Well there's this shorthand that you can do it x plus plus this will be the same same results as above.
Let's just comment that out
and you should see again values for.
So what is this.
This line here this line 20 x plus plus is the exact same as this line above.
In fact it's just a nice shorthand for this is what's called incrementing something.
And you can actually do the same with decrementing as well.
So you can actually say X minus minus will be the equivalent of X equals X minus 1.
See that she's just 2 so X would be 0 at this point.
At the end of 20 line 20 and 0 plus due to
there's also other shorthands that are kind of interesting which are these show you do you do something
like this as well.
X plus equals 1.
This will be the same as X plus plus with the same
same ones.
You also have X minus equals 1 which would be the same as X minus minus even use multiplying.
So X times equals 2 which would multiply it by 2 and reassign the value.
And you can do even to divide and modulus as well.
So that's something interesting to keep in mind just for these are just shorthands programmers very
much like shorthands because we don't want to type code all the time.
So these are the basic operations that will work for.
Double float and so on.
But for char's This also works.
So let's say we had to delete all this
and say we can't share C equals a and we said C plus plus K.
What do you think the value of c is.
If we run it it's B.
Capital B so you can actually add 1 to.
This is actually this is a single coded a.
Here is actually a number and that number corresponds with the what's called an ASCII table.
So the ASCII table will have all of the letters and numbers for the ASCII set character set and this
corresponds to a specific number.
So A is 70 something I can't remember really.
So one thing you should do after this lecture you shouldn't look up all the Ask the ASCII table and
check to see what value a has.
So in this case you should print out since we're adding 1 to a and then incrementing it at c or so.
Remember Like I said it's all interpreted differently even though it could be used similarly.
So we're using the binary operator addition here.
And this is what's called a unitary operator because it only acts on one side as plussed class a unique
OPERATOR So one unit.
You mean in French
so the last thing I want to talk about with arithmetic operations is what's called precedence.
So let's say we had our original example here
sequels five why sloven
in said Kuhn's X plus Y.
So this.
So you'll notice here what happens first.
Because you have two different Opper operators we have the addition operator and we have the assignment
operator as well.
And yet we know the results is 16.
So we know it does this X plus wife first and then it assigns it that value 16 to Zed Well like I said
this is called precedence and this this is something that all operators have in precedence is really
you can think of it as kind of the order of operations that will happen first.
So in this case this addition has higher precedence than the assignment because that's what was completed
first.
Here's another example.
So let's say we had Zed equals 10 watts 30 times every 6 or 6 minus 9
and this
sace what's the end result.
It's 181 k.
So what happens first well if you remember you're old.
Matthew probably learned something something effective bed mass which is multiplication and division
will come before subtraction and addition.
So this happens first this 36 times or 30 times 36 and then it gets divided and then you add then subtract.
Right.
So if you look up the there's also besides the ASCII table is also something called precedence table
and you'll see in the precedence table that multiply and divide are at the same level but multiply will
come first and it's higher precedence than addition or subtraction.
Now if you don't remember any of this stuff which I I rarely don't want to remember what you should
do is have parentheses around every so if you wanted this to happen first
you'll still see 181 as the result.
If say you wanted a different precedence Let's say you want to do the dition first
10 plus 30 times 36 minus 9
that give you a totally different number 8.
So if you if you don't want any kind of ambiguity you really want to use your make clear use of parentheses
to say what should come first.
This will always happen first.
This 10 plus 30 then it will multiply by 36 and divide and will subtract.
So this is very very important stuff so always just remember if you don't want any ambiguity use use
these parentheses.
And after the lecture you should go and look up the both the ASCII table and the precedence table.
See all of the operations that come for what.
So in the next lecture we'll start talking about some simple input.