Operator and Expression
Operator Precedence




Evaluation of expressions still moves from left to right, but only when dealing with operators that have the same precedence.  
Otherwise, operators with a higher precedence are evaluated before operators with a lower precedence.  
Knowing this, take another look at the sample equation:  
x = 2 * 6 + 16 / 4  
Before using the lefttoright evaluation of the expression, first look to see whether any of the operators have differing precedence.  
Here the multiplication (*) and division (/) operators both have the highest precedence, followed by the addition operator (+), and then the assignment operator (=). Because the multiplication and division operators share the same precedence, evaluate them from left to right. Doing this, we first perform the multiplication operation 2 * 6 with the result of 12. Then we perform the division operation 16 / 4, which results in 4. 

After performing these two operations, the expression looks like this:  
x = 12 + 4;  
Because the addition operator has a higher precedence than the assignment operator, we perform the addition operation 12 + 4 next, resulting in 16. Finally, the assignment operation x = 16 is processed, resulting in the number 16 being assigned to the variable x.  
As we can see, evaluating the expression using operator precedence yields a completely different result.  
Just to get the point across, take a look at another expression that uses parentheses for grouping purposes:  
x = 2 * (11  7);  
Without the grouping parentheses, we would perform the multiplication operation first and then the subtraction operation. However, referring back to the precedence list, the () operator comes before all other operators. So the subtraction operation 11  7 is performed first, yielding 4 and the following expression:  
x = 2 * 4;  
The rest of the expression is easily resolved with a multiplication operation and an assignment operation to yield a result of 8 in the variable x. 