Arithmetic Operators
An Introduction with Examples in Java |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
+
)-
)*
)/
)
Review:
Can you identify all of the statements?
Can you identify all of the expressions? (Be careful!)
/
)%
)int
int
/
(in Java):
int
value that corresponds to the
truncation of the real-valued quotient
(i.e., the quotient rounded towards 0)%
(in Java):
x - (x/y)*y
x
is the dividend, y
is
the divisor, and /
is the integer
division operatorx - (x/y)*y
is not 0
)11 / 3
6 / 3
2 / 3
11 % 3
6 % 3
2 % 3
/
and %
Math.floorDiv()
and Math.floorMod()
+
)-
)++
)--
)++x
and
--x
) or after (in the case
of x++
and x--
) using it
++
(post) --
(post)++
(pre) --
(pre)
+
(unary) -
(unary)*
/
%
+
(binary) -
(binary)=
5 + 3 * 2
2 * 4 - 1
2 * (4 - 1)
2 + 8 + 3 - 2
8 * 4 * 2
8 / 4 / 2
j
and 8 is
assigned to i
+
has the highest precedence so 3 + j
is executed first and evaluates to 3
(a value)
5
is assigned to 3
and 3
isn't a variable