- Displaying Basic Information
- Manipulating Variable Values with Operators
- Understanding Punctuators
- Moving Values with the Assignment Operator
- Working with Mathematical/Arithmetic Operators
- Making Comparisons with Relational Operators
- Understanding Logical Bitwise Operators
- Understanding the Type Operators
- Using the sizeof Operator
- Shortcutting with the Conditional Operator
- Understanding Operator Precedence
- Converting Data Types
- Understanding Operator Promotion
- Bonus Material: For Those Brave Enough
- Summary
- Q&A
- Workshop
Understanding Operator Precedence
Rarely are these operators used one at a time. Often multiple operators are used in a single statement. When this happens, a lot of issues seem to arise. Consider the following:
Answer = 4 * 5 + 6 / 2 1;
What is the value of Answer? If you said 12, you are wrong. If you said 44, you are also wrong. The answer is 22.
Different types of operators are executed in a set order, called operator precedence. The word precedence is used because some operators have a higher level of precedence than others. In the example, multiplication and division have a higher level of precedence than addition and subtraction. This means that 4 is multiplied by 5, and 6 is divided by 2 before any addition occurs.
Table 3.3 lists all the operators. The operators at each level of the table are at the same level of precedence. In almost all cases, there is no impact on the results. For example, 5 x 4 / 10 is the same whether 5 is multiplied by 4 first or 4 is divided by 10.
Table 3.3 Operator Precedence
Level |
Operator Types |
Operators |
1 |
Primary operators |
-() . [] x++ x-- new typeof sizeof checked unchecked |
2 |
Unary |
+ - ! ~ ++x --x |
3 |
Multiplicative |
* / % |
4 |
Additive |
+ - |
5 |
Shift |
<< >> |
6 |
Relational |
< > <= >= is |
7 |
Equality |
== != |
8 |
Logical AND |
& |
9 |
Logical XOR |
^ |
10 |
Logical OR |
| |
11 |
Conditional AND |
&& |
12 |
Conditional OR |
|| |
13 |
Conditional |
?: |
14 |
Assignment |
= *= /= %= += -= <<= >>= &= ^= |= |
Changing Precedence Order
You learned how to change the order of precedence by using parentheses punctuators earlier in today's lessons. Because parentheses have a higher level of precedence than the operators, what is enclosed in them is evaluated before operators outside of them. Using the earlier example, you can force the addition and subtraction to occur first by using parentheses:
Answer = 4 * ( 5 + 6 ) / ( 2 1 );
Now what will Answer be? Because the parentheses are evaluated first, the compiler first resolves the code to this:
Answer = 4 * 11 / 1;
The final result is 44. You can also have parentheses within parentheses. For example, the code could be written as follows:
Answer = 4 * ( ( 5 + 6 ) / ( 2 1 ) );
The compiler would resolve this as follows:
Answer = 4 * ( 11 / 1 );
Then it would resolve it as this:
Answer = 4 * 11;
Finally, it would resolve it as the Answer of 44. In this case, the parentheses didn't cause a difference in the final answer; however, sometimes they do.