Welcome back.
In this video we're going to look at operators and add order of evaluation.
So order of evaluation is a concept that will come in handy at the point of this point here at subtractions.
But let's have a look at the calculations in general.
So at the operators.
So we have seen that the plus sign and we've used it already.
So this allows us to add two values together as we have built our addition calculator.
Then whenever you want to do subtraction you need to consider something.
And that is the order of evaluation.
Because what happens here internally at this point is going to be that it starts from left to right.
So what it does is internally it basically writes out something like this okay.
So this now becomes a new string where the string holds subtraction num one minus num two equals.
And then whatever is inside of num one.
So in num one we have five.
So it will take this as a string now and then it will try to subtract whatever is in num two.
So it will try to subtract an int from a string.
Which is why we get this error here.
So the order of evaluation is always going from left to right I think in your case from left to right
like this.
So how do we get a way around this.
Because we're using this plus sign here which is really just there for concatenation.
It's not for calculation.
This is not an operator for calculation but it's for concatenation.
As we have seen concatenation before where we said for example control right line and we said h is and
then plus and let's say num one.
And then it would say age is five for example.
So this here is concatenation concatenation as we've used before.
Now here we are doing concatenation all the way up to this point.
So suddenly this entire statement here becomes a string.
And then we try to subtract from that string.
So let's just use that as an example here.
Let's actually take this and write another console write line statement here where we say subtraction.
And then minus num two.
So these two statements are technically speaking the same.
So in order to get our way around it we need to add brackets okay.
It's like in mathematical applications or calculations where whatever is inside of brackets is done
first before it is considered for the other stuff.
So suddenly, out of this statement that we had before a new statement arises.
So instead, what now is going to be considered here is going to be equals.
And then we have num one minus num two which is five -13, which then ends up being 8 or -8.
So this will be internally like minus or plus minus eight like so.
So this here will result in minus eight.
So now we can use concatenation again where we're not saying string minus eight but we're saying string
plus convert this into a string as well which is going to be minus eight.
So now let's run this and see if you print line statements I might have a few too many.
Let me get rid of the ones with Daisy with my cute little poodle Schmoodle.
Let me rerun my code so it says addition num one plus num two 513.
I love that it didn't do the calculation correctly either, even though it didn't give us an error.
Then we have the subtraction twice which says num one minus num two is minus eight and then we have
the multiplication.
So five times 13 is 65.
That's fine.
And then we have division num one minus num two is zero.
So let's look at that in a second as well.
Therefore first let's fix one problem at a time.
So here you see it did the same thing.
So it just said okay well I'm going to consider this as a string.
And then I'm going to consider this as a string.
So then in the end what it is is just it said five plus 13 is 513.
And I saw this really hilarious video about, um, two plus two being 22 instead of four.
And a math teacher was really that, no, this is not correct.
And yeah, all those wokeism was hilarious.
So how do we fix this?
Think about it.
Yeah, we are just using brackets around it as well.
So let's rerun this and we will see addition num one plus num two is 18.
So five plus 13 is in fact 18.
Okay now let's look at the next point.
That is multiplication.
So here this multiplication worked out.
So we have gotten the value of 65 which is five times 13.
But this for some reason was weird.
It just gave us zero.
Why is it zero.
Well because five divided by 13 is zero point.
What will it be, something like 0.38 or something like that.
So we're trying to store an int that can only hold whole numbers.
And we're trying to put in a floating point number in it.
And that will result in a very simple solution.
As we saw here where it just said zero.
So 12 divided by 13 is still zero.
So it's not going to go ahead and do some rounding up.
So let's see 12 divided by 13.
Let's see what it says.
And you see we still get zero here.
That is because we have a zero dot something something.
So it's not rounding neither up nor down.
It's just cutting the number off.
So internally if you are working with INTs so 5.3 as an int will still be five.
So if we now go and say 14 divided by 13 let's rerun this.
We will see 14 and then it says one.
That's because it's 1.07 or 0 eight still one point.
We take everything on the well after the decimal point and we cut it off, and that's the number that
we get.
So we get one as the result of num one divided by num two.
Okay.
So that was operators and order of evaluation.
You see here whenever you're using plus and minuses it is basically going from left to right or like
this.
But when you're using multiplication or division it's like in math where multiplication and division
has a priority.
So if we take five plus three minus eight times two, what this internally does.
Just a quick math lesson here for those who weren't aware.
So what you do is you take the multiplication first.
So you do 16 here.
So 16.
And then what did we have before the 16.
We had the minus.
Then we had the three and we have the five.
So now we can just go in any order.
Doesn't matter.
We can say eight -16 or we can say three minus.
Well three -16 is -13 plus five.
It will still be the same.
So addition and subtraction they end in the same result so to speak, no matter in which direction you
do it.
But whenever you do multiplication the multiplication has priority.
So basically let's have another example.
So let's say this will be -13 plus five should be eight right.
And then if we go ahead and we have the same here but like this where we add brackets in here.
So now suddenly the calculation will be five plus minus five.
I multiplied with two which then is five plus minus ten which then is minus five.
Okay.
So yeah this is basically the order in which calculation works.
Same would go with division in here okay.
But maybe that was a little too simple for you.
And you already knew that.
But whenever you see these uh, these Facebook or Instagram challenges where they say 99% of people
can't solve this and it's something like that, just do multiplication first or whatever is in brackets
first, then the multiplication slash division.
And then you do the addition slash subtraction.
All right.
So that's it for this video I think that was the longest one we had so far.
But I put a few concepts in here.
So see you in the next one.