Why do we add meal to itself the second time when equating to total?


#1

after we have re asigned the variable meal to meal + meal * tax why would we add meal to meal again?


#2

this line:

meal = meal + meal * tax

?
Well, we want to update the meal variable. What should the new value be? The cost of the meal + the tax (tax is meal cost times tax)


#3

I was initially wondering the same thing. Basically the calculation you are doing is meal = meal + (meal * tax).


#4

Well the tax will be something like 3% or something around, I assume. So if the meal costs 40$ and you multiply it with 0.03, you will have the tax for the meal only, but not how much you have to pay actually.

meal (price of the meal at the cafeteria or whatever) = 40$

tax = 40$ * 0.03 = 1.2$

meal (price you have to pay) = meal + tax = 41.2$

Makes sense, right? :grin:


#5

I'm confused as to why the total came out at 54.63, by the looks of line 7 meal = meal + meal * tax shouldn't that be two meals?


#6

No, because of order of operation. Multiply is before add/sum. Let's break it down:

meal =

we are going to assign a new value to our variable meal.
then because of order of operations, multiply is first:

meal * tax

which will give you the tax, which we then add to the meal costs:

meal + (meal * tax)

i put it in brackets, because we already calculated it. So now, we updated our variable and included the taxes