Welcome back and this video, we are going to look into operators and we're going to start with arithmetic
operators.
So I want to show you the following, the arithmetic operators that you probably have seen before,
of course, as you were in school, I guess so.
Plus minus the multiplication division and then the modulo operator.
So this might be the only one that you have not seen because it's not very common to use the modulo
operator outside of programming.
So what we can do is we can of course create a variable called result and it's going to store the result
of a calculation.
So we're using this arithmetic operator, the plus sign.
So we're adding to values.
And by doing so, what we get is eight and the eight will be stored in results.
So it's not going to store this calculation itself, but it's going to store the result in this variable
called result.
So now what we can, of course, do is we can print results.
So let's do that.
But before we do so, I'm going to comment about the print that we had before this last character one.
And let's run this and there we are.
So I had to run it twice again due to the bug so we can see how it is printed onto the screen.
Now, what we can do is we can override result with a calculation that will result itself.
So if I do something like this where as a result equals results divided by two, it will store four
in this variable called result.
So now instead of eight, it's going to print for onto the console.
And that we are it says for now, you see that I get a little warning or a tip here where it says replace
with slash equal.
So instead of saying result equals result divided by two, I can say result.
Slash equal to.
This will give us the same result.
So the same value pretty much.
So what this line here does is it says the result value is going to take its own value and then divide
it by two and then stored a new result into itself.
So in this case, it's going to take eight and then it's going to divide by two.
So that's what this legislation does.
So now, instead of using the slash, let's multiply gate.
So let's use the asterisk here in order to do the new calculation.
So it's going to take the old result and now multiply with two.
So this will result in a 16.
So test is.
And we are at this 16 here at the bottom, so that's the multiplication now, of course, we can also
use the plus sign plus equal will then add two to the whole thing.
So it should be 10.
And that we are system and of course, the same thing with a minus so minus equal to is going to be
in this case six.
So all of that is cool.
But what is this modulo, operator?
What does it do?
Well, let's see what we have here.
So let's say we want to have a result, which is going to be 15 modulo two.
Let's just test what we get there just to see what the result will be.
And we can see we get one.
So what is this one?
So what the modulo operator does is it gives us the remainder, which pretty much means 15.
Divided by two is going to be seven remaindered one.
So it just gives us the one, which is the remainder, so that the rest, so to speak, of our calculation.
If we use three here, for example, the remainder will be zero because 15 divided by three is five,
remainder zero.
So if we run it twice, we can see the remainder is zero.
Now, on the other hand, if we use for it, then the remainder will be three, because 15 divided by
four is three and remainder three.
And that we are now, of course, which also can do is a calculation like such so far A is going to
be five or B's going to be three.
And because we're not going to change them, we can also use Volle here.
So we make them immutable.
And of course the result can be A divided by B, for example.
So this works as well.
And of course, all calculations will work here.
Now, what you can see is it says the result is one, even though five divided by three should be something
like one point six six six six six.
Why is that?
Why does it not give us the right value here?
Well, that's because it says that this is an integer so well is going to be an integer and Valby is
also going to be an integer.
So if we divide integers, then we also get a integer.
As a result, an integer only contains whole values, so it cannot have a floating point value or cannot
be a floating point value.
So what we can do is we can make this one, for example, a double.
So if I do five point zero and then try to divide it by three, you can see I get an error here and
says type this match required int found double.
So now it's trying to divide a double by an integer and that creates this type mismatch.
Now what can we do about it.
Well the thing is that our result itself is an integer, so we cannot do this calculation because result
is an integer.
Now what we can do is we can either make this whole thing an integer.
So we just say to int.
By this, we make this whole expression and in teacher order result of this expression and in teacher
again, so this is a function or a method called to end.
And what it does is it converts this value into an integer that solves the problem, as you can see.
But that's really not going to give us the result that I wanted.
I really wanted this to be a double, which means I wanted this to be a floating point value, which
will give me something like one point six six.
So that's not the solution.
The solution is to change the result type to a different type or to create a new variable which contains
the value in the different type.
So what I can, of course, do is I can go ahead and say result double, for example, is going to be
of type double.
So now I can go ahead and use my result double instead for this calculation and also print results double.
All right, the other way would have been, of course, to change this variable here, but because this
variable is created and already used for something later on, changing it can be very tricky.
In our example, it's not really tricky because we are the developers of this line of code.
But let's say we are using this variable and it was created by someone else and he used this variable
at multiple different points and his application.
So then we have this situation where we are using a variable and we are changing it and maybe even changing
its whole purpose.
And then it might not work for other occasions where it was used.
So that's something that we really want to avoid.
So we just create a new variable, which will be of the right type of type double, and then we do the
calculation.
So now let's see what the result will be of this calculation.
And if we do so, now we can see we get this one point six six six six.
And then at the end it says it's seven because it just can't add more sixes because it would be infinite
amount of sixes.
But at one point it just rounds up the last value here.
All right, so that was a little look into arithmetic operators and also into types, so we have to
be careful with the types that we are using.
So I'd say we check out the other type of operators that there are and the next one will be comparison
operators.
So see you in the next video.