Usage of 2 meals


#1

In the Tip calculator lesson why do we have to add meals 2 times like “meal = meal + meal * tax” why not just 1 meal?


#2

Because “meal * tax” will calculate the actual tax that needs to be added in a meal.
e.g. if meal = 100, tax = 0.15 then

meal = meal + meal * tax
= 100 + 100 * 0.15
= 100 + 15

Therefore, a meal with tax inclusive is 115.

Hope it helps.


#3

Keep in mind precedence and order of operations. As @ss19 has pointed out, multiplication precedes addition, so meal * tax is computed first, then the result added to meal.

If you have any difficulty with order of operations in maths, then you need to do some thorough study and review. Precedence is a fundamental concept of programming that does not only apply to maths. It applies to all operations.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.