|
Remember:
Arithmetic operators in computer programs must
ALWAYS
operate on two values
of the SAME type!!!
The result of the arithmetic operation will ALWAYS have the SAME type as the operands. |
2.0 + 3 --> 2.0 + 3.0 = 5.0 (result is double) |
Examples:
7.0 / 4.0 = 1.75 (divide 2 double values, natural) 7 / 4.0 = 1.75 (7 / 4.0 --> 7.0 / 4.0 = 1.75) 7.0 / 4 = 1.75 (7.0 / 4 --> 7.0 / 4.0 = 1.75) 7 / 4 = 1 <- integer division ! |
Priority | Operator(s) | Associativity |
---|---|---|
1 |   *   /   % | left -> right |
2 |   +   - | left -> right |
6 - 2 * 5
-> 6 - (2 * 5)
-> (6 - 10)
-> -4
6 - 2 * 5 - 8 / 3
-> 6 - (2 * 5) - (8 / 3)
-> 6 - 10 - 2 [Same priority: use assoc rule !]
-> (6 - 10) - 2
-> -4 - 2
-> -6
6.0 - 8 / 5
-> 6.0 - 8 / 5
-> 6.0 * 1 [double + int: convert !]
-> 6.0 * 1.0
-> 6.0
6.0 * 8 / 5
-> 6.0 * 8.0 / 5
-> 48.0 / 5 [double / int: convert!]
-> 48.0 / 5.0
-> 9.6