(piano music)
-: In this video, let's talk about operators.
That's right.
You can perform operations on different values.
In fact, we have done that before,
but let's expand the examples and more operators.
So, basically we can add two numbers,
we can subtract two numbers,
we can divide two numbers,
we can multiply two numbers
and also we can perform an operation,
so that you will get a remainder of an operation.
Okay, so when you divide a number
and if you only want a remainder, we can do that as well.
So, let me show you those things with code here.
So, what I will do now is first of all,
let's create two variables
and of course we can name any variable name.
Let's go with integer num1,
and the value, it will have, let's say seven.
And then let's have one more variable here,
which is, let's say five, num2 is five.
And then I can perform some operation
and I can save that operation in a result.
So let's say if you want to add two numbers, we can do that.
We can save the output in result,
and then to perform the operation,
we can use a plus operator.
That's right, we can actually use a plus operator
to add two numbers.
And then once you have done that,
we can simply come back here.
We can say system dot O, dot print, Ellen,
and here we can print the result
and just to see if it is working.
And as we discussed before,
this is not a good way of running a code.
You have to first compile the code, then run.
But we got a shortcut, I can simply use Java space file name
with J, with dot Java.
Say Enter, and we got 12.
So when you say seven plus five, we got 12, cool.
Now what I will do is, so we can add two numbers.
What about supply action?
Let's try that.
So I will say minus symbol and run.
You can say we got two.
So just working.
So we can add two numbers,
we can supply two numbers.
How about multiplication?
We can do that.
So we can say star num1 into num2.
Come back here.
Run, you can see we got 35.
So seven into five is 35.
That work, that is working.
How about division?
Let's try that.
So the moment I said division, we have
to use a slash.
Okay, so we don't have a division
in fact, that's how we do, right?
We underscore or something with a slash.
So yeah, we got slash, let's try and you can see we got one.
That's right.
One is a question here, right?
So when you divide seven by five, you get two things.
One is a question, which is one,
and then you will also get a remainder.
Now in this case, when you use slash,
you will only get a question, not a remainder.
So how will you get a remainder?
In that case you have to use a modulus operator.
So we can use, when you say percentage symbol like this,
it is a modus operator that
what it will give you is only a remainder.
So if I run this code, you can see we got two,
two's a remainder here right.
Now We can use any complex numbers as well,
whatever number you want to perform with,
and I would suggest you to try a certain example so
that you'll get the, get used to this operations.
Now all this operation actually makes sense, right?
These are very simple stuff to understand.
Can we use some other operators as well?
Let's try.
What I will do now is let me just remove this result
for time being, or maybe I can just comment this part.
I also don't need this num2, let me only focus on the
first variable, which is num1.
Now, let's say I want to add this num1 by two.
So basically the value of num1 is seven.
I want to increment the value by two.
So what we can do is we can say num1 is equal to.
So basically we have to use num1,
which is num1 in this case,
and we'll add a value to it, which is two.
And then whatever new value you will generate
after this will be assigned to num1.
Okay, that looks cool, right?
Let's try this.
I will say num1, between num1,
and let's run this code.
You can see we got nine.
So this is working,
so seven became nine.
So just working right.
Now when you know
that you are adding the number by itself, okay,
what you can do is you can just use a shortcut.
Now what that shortcut says is,
let me just comment this section here
and let's write it fresh here.
So we'll say num1, see you want to increment the value
of num1 by two, right?
You want to add itself by two.
So you can say, I want to add myself, but how much value?
So I have to say equal to two.
So what we're saying is we want to increment num1
by two, even this should work.
Let's try, oh, we got an error
because I forgot the semicolon.
And let me just clear this, run.
And you can see we got nine.
So this is working, cool.
Now it's not just variation, we can do that
for any other operation.
Maybe you want to subtract a number by two.
So we can say subtract equal to two.
So it will reduce the value by two.
You can see got we got five, we can multiply
with some other number.
Maybe I want to multiple the same number
with another number and assign it.
So we've got 567 into 856.
We can divide as well.
And so you can use slash
and all the operators, which you have.
Awesome, so this is shorthand.
Now going back to the additional operator,
and then let's say if you want to add it by two,
we can simply mention the value two, right?
We can also add it by one.
So in this case, if you say added by one, the value
of num1 is seven, and then you are adding it by one.
You can see we got eight.
Now in fact, there's a shortcut for this as well.
What you can do is instead of using num plus equal to one,
you can also say num1 plus plus even this is possible,
let's remove the space in between.
So even this is possible, you can say num1 plus plus,
which is incrementing.
So what we are doing here is something called increment.
And if you're on this call, you can say we got eight.
So this operation, let me just make it one.
So all this operations are similar.
Okay, so why I'm saying similar, why not same?
We'll discuss that in some time, but this is similar
and maybe we have done that before as well.
When we talked about characters, you know,
we remember C plus plus, we done that.
So yeah, we are incrementing here as well.
Now, not just incrementing, we can do decrement as well.
So instead of saying numb plus plus,
we can also say num minus, minus.
So what we are doing now is decrement.
So when you say plus, plus, that is increment.
Let me comment this part.
And here I will say num minus minus.
Now why you have
to commend this, but I want to run this.
So we can say now minus, minus,
let's run this code if, see if this works.
And you can see we got six.
So that's how you can perform this addition,
subtraction, multiplication, division,
and increment, decrement.
In fact, we have also talked about modulars, which is
for finding the remainder.
Okay, now that was about increment decrement, right?
We have talked about that.
Now, instead of using plus plus we can actually use
something called, let me just write it in comment.
So instead of saying num1 plus plus,
we can also say plus plus num1.
You know, just to simplify this example, instead
of saying num1 everywhere, let's say num, so
that maybe you will not get, you will get confused by
that one extra symbol there, which is one,
just ignore all this operation, just focus on this part,
which is num and num.
Also, remove this, also remove this.
Let's see if this is working.
Okay, this is working.
So basically we can also say instead
of saying num plus plus, we can also say plus, plus num.
Now what's the difference between this two?
Even this also work.
Let me just try that and show you.
You can say we got eight, we are incrementing the value.
Even this will give you eight.
But what's the difference?
What's the difference between writing this afterwards
and writing before the variable?
So basically we have a different names to it.
So whenever you perform operation
after the variable, maybe it is for additional
or supply, it is for increment or decrement.
So when you do it post, we say it is post increment.
And when you do pre
before the variable, it is called pre increment.
Okay, so when we are doing it afterwards,
we say post increment.
When you say before it is doing pre increment,
now you might be thinking, hey,
ultimately the value is same, right?
So even if you pull, even if you do this
or this, both will increment the value.
Then why we have two different syntax for it.
Why not just simply one?
The answer is it behaves differently when you try
to assign a value, when you try to fetch the value,
let me repeat, these two operations will behave differently
when you try to fetch the value.
Okay, let me tell you what I'm talking about.
Let me show you it here.
Let's come back here
and let's say I have a different variable, which is int,
and then I'm storing that in result
and what I'm storing is num
or maybe I will say plus, plus num.
Okay, now think about this and maybe you can pause the video
or whatever you wanna do.
Just think about what will be the value of result.
I will say result here to print,
and maybe you have done your thinking, let's run this
and you can see we got eight.
So what is happening here is the initial value
of num is seven.
And then you are saying plus, plus plus num.
That means you are incrementing it, that
incremented value will be assigned to result.
And then you got result, which is eight.
So initially I've also mentioned
that this two will be incrementing.
So we can also say num plus plus, even they should
be doing the same thing, right?
Let's try, oh, there's a twist here.
So plus plus num and num plus plus are not same.
I mean it works same with their individual statement,
but when you try to fetch the value from them when they're
performing the operation, they behave differently.
So let's understand why it is called pre increment
and what is called post increment.
So whenever we use assignment operator with this operation,
what it will do is it will check, hey,
we have an operation which is num plus plus,
but that's a post increment.
So first it will fetch the value of num, let me repeat.
It will first fetch the value
and then increment.
In fact, it looks like one statement,
but we have two statements here, right?
In fact, we have this statement here,
num one equal to num plus one.
So what it'll do is it'll first fetch the value of num,
which is seven in this case,
and then it will increment the value of num
and will make it eight, okay?
But then what is assigned to result is seven itself,
and then you are printing that seven here, okay?
But what will happen when you
say, let me just write it here.
So we'll say end result is equal to plus, plus num.
In this case, what will happen is it will first increment
and then fetch the value, okay?
So it will first increment itself
and then it will fetch the value.
And that is what is going in result.
I hope this makes sense, right?
So this is how your code works when you say plus, plus num
and num plus plus, in fact, we'll do some more operations
with this once we get started
and it will make much more sense later.
So in the operators, we have talked about other
thematic operator until this point.
So whatever option you have done, it is
for the Arithmetic purpose, right?
Addition, subtraction, multiplication, and division.
But what if you want to compare to values?
What if you want to have compound conditions?
Okay, what are those things, we'll talk about
that in the upcoming videos.