Order of Mathematical Operations
aka Operator Precedence
Now that you have reviewed the kinds of math you can conduct with varying computer data types, you can address the issue of how a mathematical expression should be evaluated, or resolved. Given the way the variables are set up in the expression, you might get a different answer depending on how you conducted the arithmetic. Consider the following ways that the arithmetic might be implemented; parentheses are used to show how the numbers might be organized to get to the differing results.
Calculation
(5 * 3.99 + 3) * 2.99 = 65.62
(5 * 3.99) + (3 * 2.99) = 28.92
5 * (3.99 + 3 * 2.99) = 64.8
If you are already familiar with the concept of order of operations, this will be easy for you. If you are not familiar with this concept, it will be easy anyway. It is a simple set of rules for conducting arithmetic operations on expressions that use a combination of operators.
Order | Rule |
---|---|
1 | All parenthesized operations occur first, using the following rules, as needed |
2 | All operations are conducted from left to right, using the following rules, as needed |
3 | multiplication, division, and modulo arithmetic are conducted next |
4 | addition and subtraction are conducted next |
5 | the assignment operation is always conducted last after all the operations in the expression have been concluded (i.e., everything on the right side of the assignment operator is conducted before being assigned to the variable on the left side) |
Note that these rules work for all mathematical operations, including integer, floating point, and character.
With the above rules in mind, the following expression:
totalDrinkPrice = lemonadeDrinkers * priceOfLemonade
+ numberOfColaDrinkers * priceOfCola;
would resolve to 28.92 as follows:
First, from left to right, and implementing the multiplication first, lemonadeDrinkers would be multiplied by priceOfLemonade (5 * 3.99) and numberOfColaDrinkers would be multiplied by priceOfCola (3 * 2.99).
These two subtotals (19.95 and 8.97) would then be added as the last step.
Thus, the value 28.92 would be assigned to the variable totalDrinkPrice.
Short exercise. Try adding the following statements using the present program statements as examples.
totalDrinkPrice = ( LemonadeDrinkers * priceOfLemonade )
+ ( numberOfColaDrinkers * priceOfCola );
totalDrinkPrice = LemonadeDrinkers * ( priceOfLemonade
+ numberOfColaDrinkers ) * priceOfCola;
Create a new double variable called priceDifference, then calculate the following.
priceDifference = ( LemonadeDrinkers * priceOfLemonade )
– ( numberOfColaDrinkers * priceOfCola );
priceDifference = LemonadeDrinkers * ( priceOfLemonade
– numberOfColaDrinkers ) * priceOfCola;
Compare your results, and then identify the correct solution, and remember that you can always write a short test program to check your work. When it comes to developing complex mathematical statements, you should always observe the following two rules:
- Break your expression down into subparts so that each is easier to read and understand, and so that you are less likely to accidentally alter the desired expression
- If it is difficult to effectively break up the statements, it is a good idea to use parentheses in your complex statements for the exact same reasons as provided for item #1.
Finally, if you added one more double variable called unknownVariable and didn't assign anything to it, what do you suppose would happen? The output actually might be zero, but this is very unlikely. In most compilers and IDEs, the value found in an uninitialized variable (i.e., a variable that has not in some way been given an appropriate value) will be whatever was last in that memory location which of course is totally useless. There are some programming languages that zero out any newly declared variables but first, you should never trust this - compilers can miss things, and second, you should keep yourself in the habit of assuming variables are not initialized. In fact, in the C programming language, they are not initialized.
In the programming business, the value found in a variable such as this one is called garbage. It has nothing to do with your program and will only cause problems if you use it. Always make sure that your variables are given an appropriate value before they are used. The values may come from some processing as was done here, it may come from some input operation, or it may come from a simple assignment. No matter where it comes from, make sure there is some legitimate value in every variable you use.
The rules and actions provided in this section will assist you with conducting your mathematical operations as well as supporting some decision-making and repetition activities. While none of these rules or actions are difficult, they are important. Make sure you understand and can apply these rules to many different kinds of mathematical expressions.
Once again, watch and practice with this video. The more little programs you write, and the more you exercise your learning - like order of operations rules - with the programs, the stronger you will be.