Order of Operations
Most operations will work from left to right.What does that actually mean, though?
This code will display 12. It arrives at this number by performing the + operation twice. The first time, it adds 3 and 4 together. This combines to create the number 7. In technical terms, we would say, "3 + 4 is an expression that resolves to 7".
That expression, once resolved into a 7, is then added to the 5. The computer adds the numbers 7 + 5 to create 12.
Mathematically, addition can be performed in any order. But you must ignore this fact. Computers are methodical beasts and will always do things in a specific way. In this case, 3 + 4 goes first, and only then does it compute 7 + 5. It would be factually incorrect to say that 4 + 5 is an expression that resolves to 9. Because this is not a computation the computer ever encounters when running this code.
The interpretation of this code always works from left to right.
There are, however, two exceptions to when the program will do things in a different order.
Parentheses
If you want the code to run in a different order, you can always wrap expressions in parentheses. This tells the compiler that you want to resolve some set of expressions first before including them in another expression.In our example, we could have instead said:
In this case, the program actually computes 4 + 5 as an expression first before attempting to add it to 3. Obviously we still get the same result, but internally, we've changed how the computer processes these numbers.
The usefulness of this is more evident when we mix addition and subtraction:
The first line computes 3 - 4 as an expression. This creates the value of -1. Then we add 5 to -1 in the full expression. This creates and prints the value 4.
The second line, despite being the same numbers, is grouped differently. The 4 and the 5 are added in an expression first to get 9. That expression is subtracted from 3 to get -6.
Parentheses are free! You can use as many as you want, especially if it makes things more clear.
The following code is not clear, but is an extreme example that is technically valid:
Operator Precedence
The second exception to this rule occurs when there are different types of operations that have a different precedence. Some operations have higher priority than others. If you remember your Algebra from school, you may recall that multiplication has a higher priority than addition. This is also true in programming.Is this a 7 or is it a 9? If you perform the operations strictly from left to right, it's a 9. But if you run it, it's a 7. This is because the multiplication (*) takes higher priority than addition and is computed first.
When identifying the expressions, you would say "2 * 3" is an expression that resolves to 6, and then 1 + that combined expression resolves to 7".
It would be incorrect to say "1 + 2 resolve to 3, then that expression times 3 is 9".
Values are always computed left to right
This may seem like a contradiction to the above, but it is a subtle technicality. If you want, you can ignore this bit for now.The values in an expression are always computed from left to right. However, it is just the operators that combine the values that change their order based on operator precedence and grouping.
Consider 1 + 2 * 3 again.
The computer does not say: "2 times 3 is 6. 1 + 6 is 7."
What it actually says is: "1 +...oh wait, hold on, 2 * 3 is 6, ah yes, those add together to make 7."
It consumes each value one by one from left to right, but it will hold on to values temporarily until the full expression is ready to be resolved.
This will not make a large difference in how your code runs yet (but it will later). Additionally, as you watch the real-time visualizer run the code, it explains why the lower-priority operators are having their left numbers analyzed first.
Precedence levels
Multiplication has higher priority than addition. But what about the other two operators that we know about?Addition and subtraction have equal priority. This means that the compiler will strictly combine expressions with addition and subtraction from left to right, unless there are parentheses. This is called the addition group.
Multiplication and division also have equal priority. This means that the compiler will combine * and / expressions from left to right, unless there are parentheses. This is called the multiplication group.
The multiplication group has higher priority than the addition group.
As we learn other operators, their operator precedence will be mentioned.
When in doubt, add parentheses
When you write code, you will have some sort of conscious intention you are trying to convey. The easiest thing to do is to add parentheses to clearly indicate that intention, even if it's redundant. This leaves little wiggle room for the compiler to interpret things incorrectly, or worse, another programming reading your code to interpret things incorrectly.In other words, the best option is to write code like this: You will eventually get an instinctive feel for how the operator precedence levels will group things. Then you can more confidently omit the parentheses. But for now, focus on expressing original intent.