|
|
|
|
|
|
|
FREEZE_PT and BOIL_PT are added first, and then their sum is divided by 2.0. We evaluate subexpressions in parentheses first and then follow the precedence of the operators. |
|
|
|
|
|
|
|
|
When there are multiple arithmetic operators with the same precedence, their grouping order (or associativity) is from left to right. The expression |
|
|
|
|
|
|
|
|
means (int1 int2) + int3, not int1 (int2 + int3). As another example, we would use the expression |
|
|
|
|
|
|
|
|
(float1 + float2) / float 1 * 3.0 |
|
|
|
|
|
|
|
|
to evaluate the expression in parentheses first, then divide the sum by float1, and multiply the result by 3.0. Below are some more examples. |
|
|
|
|
| Expression | Value | | 10 / 2 * 3 | 15 | | 10 % 3 - 4 / 2 | -1 | | 5.0 * 2.0 / 4.0 * 2.0 | 5.0 | | 5.0 * 2.0 / (4.0 * 2.0) | 1.25 | | 5.0 + 2.0 / (4.0 * 2.0) | 5.25 |
|
|
|
|
|
|
Type Coercion and Type Casting |
|
|
|
|
|
|
|
|
Integer values and floating point values are stored differently inside a computer's memory. The pattern of bits that represents the constant 2 does not look at all like the pattern of bits representing the constant 2.0. (In Chapter 10, we examine why floating point numbers need a special representation inside the computer.) What happens if we mix integer and floating point values together in an assignment statement or an arithmetic expression? Let's look first at assignment statements. |
|
|
|
|
|
|
|
|
If you make the declarations |
|
|
|
|
|
|
|
|
int someInt;
float someFloat; |
|
|
|
|
|