In this video we're going to be looking at the different operators that are available to us in C sharp.
No I already went ahead and created the project and I call it C sharp upper it does once again try to
avoid any special characters when naming your projects and solutions.
And so I just use the word sharp instead of the the special character which looks like a no sign right.
So C sharp operators and I already took the liberty of kind of writing in some things in the main function
so you can pause the video try and create the project yourself and put you in these comments and then
you can resume when you are done.
No operators are symbols that are used to performed well literal operations art.
So in any programming language you have access to these operators when you want to do things like math
and all the other categories where they're loathe to do things that comparisons but for this introduction
to operators are just going to be dealing with the ones that are probably more mathematical and we need
to know if nothing else.
So let's start off with the basic assignment operator.
So we have used this already in previous examples and that is used when we want to give some variable
of value.
So if I have a variable it's into num then I want norm to have the value of five then I want to say
num you are assigned the value five.
So that's really what that is it's really an assignment.
So 1 equals sign by itself is really seen as an assignment.
All right.
So we're putting the value of five inside of our variable whereas signing the value of five to our variable
No.
All right.
So that's the basic and very easy one and that we can always just write a console.
That's right.
Line just to prove that it works.
So I'm just going to say assign assigned value to variable.
All right.
And then I'm going to use my interpolation.
So we should know how to concatenate string already.
So since I have this one of villains I mean this is the one that you're going to use more frequently
than not.
Otherwise use the one that's your language or your Net framework supports.
So once again we use this to assign a value to a variable it can be multiple variables with multiple
assignments.
It can be a static value so static means that I have called it in meaning I put five.
So this will never change unless I come back to this screen and change the stakes from five to 10.
That's the only way this will change.
But then that's what makes it static whereas when we're assigning values from the user input it makes
it more dynamic and the principle of assigning doesn't change.
All right.
Now let's move on to the arithmetic operator.
So for arthritic operators they're usually used to perform mathematical operations.
So I'm going to define two variables are I know int x and then I'm going to show you that you can actually
do a bit curious on and assignment in one line so I can say X is equal to five and into Y is equal to
10.
Right there.
So instead of splitting it into the lines I could just see int it whatever variable is equal to whatever
value I want and do that in one line.
So that's another week into your assignment.
Right.
But for this section I want us to do some math.
So I'm going to go through different examples that we're going to print to screen the different outcomes
of the math that we're doing with these.
So I'm going to say console dot console dot right.
The line is equal to.
And then for each line I'm going to in to use my interpolation.
So I went to see a dollar sign open and closed quotation marks and then I went to start off with additions
but we're going to go through the different examples additions and then I am showing the result of an
addition operation between x and y so to add X and Y we're literally going to use the plus side.
So anyone with a plus saying can be used for concatenation reasons also but it's a form of what we call
polymorphism where the plus sign has multiple purposes.
The plus sign is seeing here that I'm adding two numbers.
So do some math whereas in the context of a string when you see block of string plus string variable
it seeing I'm adding two strings so technically it's the same addition but the outcome is going to be
different because we don't expect to see 5 1 0 at the end of this operation we expect to see 15 where.
Whereas if there were strings then you would see 5 1 0.
So adorable to just know that this is arithmetic and this is how we're adding two numbers.
So that's all we add and I'm just going to duplicate this line because I want to show a subtraction
multiplication division and then this one called modules.
Right.
So I'm just going to duplicate this console of that right line by holding down control and pressing
D.
So that's a nice quick way to duplicate your lines in Visual Studio and then I'm just going to change
this also.
This is subtraction and then subtraction it's the minus sign.
All right.
So we're adding x plus y and they were subtracting X minus Y so anybody who understands the basic mathematics
this should not be too all tough place for you to understand.
So multiplication we use the asterisk.
We don't use the X..
You know when we are writing it on paper we use X but as you can see X is going to be seen as x.
So if we want to operator that handles multiplication we need to use the asterisk or the star.
All right.
If we're doing division then what.
Most if not all keyboards and I was just going to go out on a limb and say no keyboard has the division
unsigned the way we write it on paper.
So far division have to use your slash so the same slash you use for the comments is the same slash
that you use for division but obviously you're using one of them because if I use two then that's going
to start some commenting right.
So one slash that is division.
So this is saying that X R 5 is going to be divided by Y or 10.
All right.
We do have modulus and then modulus is the percentage side.
So this is basically our division within this modulus is going to return the remainder.
Right.
So for instance if I said 2 divided by a 1.
You know what that me actually change the told so I can.
When we print details the screen you see exactly what I'm seeing.
So let's say this is three and this is two.
All right.
So when we see three divided by two we're going to get back one point something right.
That's the one point five.
That would be the decimal.
Right.
But then if I said modulus it's going to return one because 2 goes into three one time with our remainder
of one.
All right.
So that's the difference between the division and the modulus the modulus returns the remainder of the
division operation and division well just tries to divide so I'm going to pause right here and then
we're going to run this operation and see exactly what's happening.
And so we we usually go over here in our solution explorer or actually our project and say it's at the
start of project but you've probably figured out the shorter we had to do that by no.
And that is by selecting from this dropped only.
So right beside the start button there's a dropdown list with all the projects and you can just see
which one you want.
And then go ahead and start.
All right so on screen we're seeing the results of our arithmetic operation.
So we assigned the value.
That's the first one.
And then addition is five plus three plus two is five subtraction is one multiplication is six division
is one.
And modulus is one.
So because they're both returning one you're not really seeing the pull off modulus.
So I'm going to change this and I'm going to use and when to switch these numbers are on someone to
use three and I'm going to use value like eight.
So at least we know that when we hit the modulus we won't be seeing the same for the division and the
seeing for the modulus but actually you will be sorry if my math is my my slip no.
Let me try.
Eleven so eleven divided by three is going to be three times but then the modulus is expected to return
to because that's the remainder from this division operation.
So let's try that again all right.
So here we go.
So we are getting different values because well once again it's X and Y and whatever we put in as of
others of X and Y will influence the math being done.
So three plus 11 is 14.
Three from 11.
That is eight or if I'm to read it exactly how we wrote it.
That's eleven minus three leaves eight multiplication that is 11 times three division that is 11 divided
by three is three.
So it goes into it three times.
I notice it's returning just an INT.
All right.
And then I say give me the modulus.
So it's returning home many.
You know the remainder of the division operation of eleven and three no right now we're adding two variables
what if we wanted to add a variable to the value it had in it's of as well as another value.
So for more context I wanted to add whatever X is and add four more to it.
So whatever value X has.
I want to add forward to it.
And that should be the new value of x.
So that means I would have to do something like X is no assign the value or X is equal to itself to
loss for right.
So I'm doing this after this operation so 11 3.
Those are the values.
And then the math will be done with the values of eleven point three.
But then I want to change the value of x to be four more than it was when it was first initialized.
Right.
So now I'm adding four to whatever value X had because X is a variable I don't know what it has at any
point.
So I have to retain it in the math.
And then after that math is done I assign it to itself so that with that done I'm just going to use
this console right line and then I'm going to see a new value of x and then I'm just printing X to the
screen and then just to prove that that gave it a new value I'm going to print reprint all of these
additions operations are sorry all of these arthritic operations using X and then the expectation is
that the values that the initial operations would have yielded should be different from these values
after we've changed the value of x so let's try that.
So now we can take a look and we see that okay all of these values were the same as we saw last time
but then the new value of x is 15 because like I said we added four to whatever value X had and then
it will work all different math because the value of x is different.
Notice the division isn't all five because three goes perfectly into fifty and five times and these
nor I mean those no operations like this are done all the time most you know you don't always know what
value is there so you just retain the variable and do whatever addition or subtraction and multiplication
it can be done with any other operators.
If I wanted to multiply the value of X by four then I just say X is equal to its of times four and then
that's its new value.
No because of that function or the need to do that kind of function we have what we call compound assignment.
So instead of having to type of x is equal to x plus four then we can actually do something like x plus
equals five.
So I'm just going to answer it for.
Since we're using for it let me use for all the time.
So I'm just going to copy this down here and show you that these two lines are seeing the same thing
X plus equals four is the same thing as saying X is equal to x plus four and the same applies for any
other operation that would be carrying out.
So if I wanted to see X should be equal to x times five.
All right so X is new value should be itself whatever value it has at the time.
Times five whatever this use should be assigned to X.
That's the same thing as seeing another right.
The longer we in comments initially the shorter we x star equals five.
All right.
So any time we want to do that kind of operation where we're assigning the variable a new value based
on what its value is already then we can rewrite it either way.
We can write it as X is equal to x times five.
Or we can see the variable which is x in this case that operates all equal.
I notice there's no space there's no gap here.
If you put that space there you might get an arrow.
So it is operator assignment operator.
And then the value and then that would imply to C sharp that you're doing this operation.
All right.
So I just wrote out some examples of assignment operators who can take some time and write these off.
But what I'll be doing here is changing the value of X with each operation just showing you that whatever
the operation is with the arithmetic symbols can be used for the compound assignment.
And as long as you just have that operator and then the equals sign.
So let's try it again.
All right so we have our first block where we did our assignment.
It is the value was 11 and then we did the operations eleven and the three I believe.
Yeah.
And then we did the operations with the three and eleven and then we changed the value of the x variable
to 15.
And then we did new operations and then by the time we got down there the value was still 15 because
we didn't change a value again until I said X plus equals five.
So 15 plus five is 20.
Then I said X minus equals three which means whatever value I have no which is 20 I want it to be equal
to that value minus three.
So by variables value got set no to 17 because it was 20 minus three.
And that's the new value of X then we go on to say whatever the value of x is I want to multiply it
by two.
So I am saying 17 times two is no thirty four.
Then I say okay whatever value that has make it make x no equal to that divided by three and thirty
four divided by three is pretty much eleven.
And then we went on to say give me the modulus of three into eleven and then that gave us the two.
So that's just how we handle compound assignments and arithmetic operations.
So like I said these are not all of the operators that are available to us in C sharp.
And by extension any other programming language but this is a good place to start.
And as we go along look at if statements and for losers start seeing other operators use for other things.