4.12 Increment and Decrement Operators
Java provides two unary operators (summarized in Fig. 4.14) for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++
, and the unary decrement operator, --
. A program can increment by 1 the value of a variable
Fig. 4.13 Arithmetic compound assignment operators.
Assignment operator | Sample expression | Explanation | Assigns |
---|---|---|---|
Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; | |||
+= | c += 7 | c = c + 7 | 10 to c |
-= | d -= 4 | d = d - 4 | 1 to d |
*= | e *= 5 | e = e * 5 | 20 to e |
/= | f /= 3 | f = f / 3 | 2 to f |
%= | g %= 9 | g = g % 9 | 3 to g |
called c
using the increment operator, ++
, rather than the expression c = c + 1 or c += 1
. An increment or decrement ...
Get Java How to Program (early objects), 9/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.