Welcome back sometimes in our programs we need to do a little bit of math.
For example we might need to take some variable and increase it by a certain amount or decrease or multiply
or whatever.
For example you may have a game and we have a character that has a health value.
And if the character gets damaged well then you need to decrement or increment that health value by
some amount.
And so normally the way you would change the value of some variable is by the following and you would
take some variables say you have an integer called I and you wish to add 3 to that integer.
You would do so by say my equals on plus 3.
You see with this line of code does is it works from right to left.
That's kind of how the assignment operator works.
It takes whatever's on the right side and assigns it to the variable on the left.
Now if you were a mathematician you would kind of cringe at this because how could I be equal to itself
plus three.
That's impossible.
Well in programming the equal sign doesn't mean is equal to.
Remember that's the double equal sign.
The single equals is the assignment operator which takes some value to the right and assigns it to the
variable to the left.
And you can use a variable for its current value to assign a new value to the same variable because
it works from right to left.
So say the value of i is 5 and you want to increase it by 3.
So what this means is you would take the value of pi which is 5 x 3 to 8 which now gives you a value
of 8 and the assignment operator takes that value of 8 and overrates I.
So now it's equal to 8 when it was previously 5.
So that's how you change the value of the variable y.
So what if we could think of some sort of shorthand to make our lives a little easier instead of having
to write very little equals variable plus 5 or something.
What variables are very long like my very long variable name or something.
So the ridiculously long and sometimes they get pretty long.
You don't want to have to write my very long variable name equals my very long variable name plus five.
You want some sort of shorthand and this is how you do it.
The plus equals operator so that plus equals operator will take whatever variable on the left you have
and and the thing on the right to that variable so it changes the variable.
Think of it as kind of like a function.
And it has two input parameters.
They just happen to be on the left and right of the operator.
That's really what operators do.
It's a it's known as a binary operator because it takes two input parameters namely what's left and
what's on the right and what the result is is the changing of the variable on the left.
So I plus two equals three is the same thing as I equals plus three it's a shorthand.
And there are a handful of the shorthand operators of these binary operators that you can use in arithmetic
in your code.
So say you have an integer called I and you've initialized it with the value of 1 if you wish to just
simply add one to I.
You can say I plus equals 1.
And now I equals two.
There's also similar operators for subtraction multiplication and division and the modulus and we'll
get to what the module is.
So I plus two equals one we'll add one to I.
So now I equals to say now we say minus equals 1.
If I was to before you subtracted 1 from it now is just 1.
So now the next line if we do the times equals operator with just the asterisk and an equal sign when
I is one.
If we say i times equals 6.
Well now we're multiplying by 6 so now one time 66 so I's now equal to 6 then we can say the divided
by equals operator.
And if I was 6 and we said slash equals three or we're taking six dividing by three and storing the
result back in.
So now it is 2 again.
And then the modulus operator this works exactly the you know divided by sign equals 1 is kind of like
taking a modulus 1 and returning the result.
Now what's a modulus a modulus is very simple it sounds complicated but all it is is a remainder.
Where does it take the number to the left of the percent sign and the number to the right and performs
a division.
And if there's a remainder then that remainder is the result.
If the remainder is 0 then of course 0 is the result.
So take for example two divided by one
or anything divided by one is itself so to divided by one to two with the remainder of zero
so if I took the modulus operator with two and one which would be two per cent sign one this would evaluate
to zero.
So that's kind of how it works if I took three modules to use three modules two it would take three
and divide by two.
And what's the remainder.
Well tune goes into three one time with the remainder of 1.
So this expression evaluates to 1 so that's all the modulus is is a remainder.
When you take the division of two numbers.
So the modulus equals operator is just that it takes the modulus of and one takes the remainder of that
and then assigns it back to.
So now becomes the remainder of I and one or whatever number that you wish to use there.
So that's kind of the modulus operator.
Not quite as widely used as maybe some of the other operators but it's there and you may find some clever
uses for it in your programs.
Now the increment operator with the number 1 is sometimes very useful.
And it happens all the time in your code sometimes you might need to increment by one many many times
and so would be even easier to do that if you had an even shorter shorthand for that.
And this is where the plus plus and minus minus operators come in the plus plus and minus minus.
Operators are just that they they they are identical.
Two plus two equals one.
So if you want to increment your variable I want to use the plus plus operator.
Now they come in two flavors.
The pre-increment and the post-increment operator the pre-increment operator is plus plus I have some
variable i is an integer and you wish to incremented by one.
What you do is you write plus plus I.
And the end result is I guess incremented by 1 the post-increment operator plus plus also ends up with
I being increased by 1.
Now there's a key difference between these two operators and it's very important to know the difference
between the two.
So you know when to use each one.
Plus plus I operator or plus plus some variable.
What it does is it first increments.
I buy one.
So I becomes whatever it was before plus 1 then it returns that value.
So it's kind of like a function that takes I as one Two it changes I and then returns the value of the
new guy on the other hand the post-increment operator is actually very different in what it does is
it first returns the value of i before you incremented it at all and then I becomes increased by 1.
So this line actually returns what I was before you increment today and then after that line is done
I now equals itself plus 1.
There's actually one has been added to.
So the pre-increment and the post-increment operators are actually quite different although they both
end up in the same situation with the same net result.
I guess incremented by one.
And this could be any variable you want.
It does have to be in a variable that can be incremented by 1.
So you know you would think that something like a char wouldn't work but actually a chart is an integer
if you recall from a previous video the letter A has an integer representation and if you have a char
variable and you incremented by one you're just going to get the next char in the list if it is if it's
defined you're going to get the next char with the integer representation of one more than what it was
before.
So not something you're really going to get into but just keep that in mind if you pre-increment or
post-increment a char it's going to give you maybe something you wouldn't expect.
So these are the increment and decrement and other types of arithmetic operators and hopefully they'll
aid you in your programming and give you some some tools to sort of Shorten down your code and make
it look a little more efficient more readable and overall give you a better programming experience.
Thanks a lot.