2.3 Increment and Decrement Operators
In addition to the basic arithmetic operators, Java has two useful operators
that increase or decrease the value of a variable by one. The increment
operator, ++, adds one to a variable while the decrement operator, --, subtracts
one from a variable. Both operators can be used in two forms: prefix
form, with the operator preceding its operand and postfix form, with the
operator following its operand.
Example 1The statements
n++; and ++n;
both have the same effect as the statement
n = n + 1;
The statements
n--; and --n;
both have the same effect as the statement
n = n - 1;
|
|
The difference between the prefix and postfix forms only appears when
these operators are combined with other operators. In this text, we will
never use expressions that combine increment or decrement operators with
any others so, if you do not read the rest of this section, it will not cause
any difficulties. If, however, you are curious about the difference, read on.
When an expression containing an increment or decrement operator
is encountered, two things occur: the value of a variable is increased or
decreased and the expression is evaluated. The difference between prefix
and postfix forms is the order in which these operations are performed:
1. If a variable has a prefix operator, the variable is incremented or
decremented first and this new value is used as the value of the expression.
2. If a variable has a postfix operator, the variable is incremented or
decremented after the old value has been used as the value of the
expression.
Increment and decrement operators have a higher precedence than any
of the other arithmetic operators. When combined with other arithmetic
operators, increments and decrements are performed first unless parentheses
change the order of operations.
Example 2Suppose that we have the declaration:
int i = 2, j = 3;
If we then write the statement
i = (j++ + 4) * ++j;
evaluation proceeds as outlined in the following table:
Evaluation Stage Expression Value i j
Start i = (j++ + 4) * ++j 2 3
Evaluate j++, increment j i = (3 + 4) * ++j 2 4
Increment j, evaluate ++j i = (3 + 4) * 5 2 5
Add 3, plus 4 i = 7 * 5 2 5
Multiply 7 by 5 i = 35 2 5
Assign 35 to i 35 35 5
The final values of i and j are 35 and 5 respectively.
|
|
Although it is perfectly correct to write expressions like those in Example
2, we do not recommend it. In general, we suggest that you only use
these operators by themselves. This may involve a bit more writing but it
will likely lead to greater clarity in your programs.
Exercise 2.3
- Suppose that the following declarations have been made:
int i = 3, j = 2, k = 5;
Using these starting values in each part, find the value of each variable
after the given statements have been executed.
(a) i++; (b) ++j;
j = --k; k = i++ + --j;
(c) j = ++i * i++; (d) k = i++/j--;
(e) i = ++k * j--; (f) k = (2 + i++)/(3 + ++j);
- Suppose that the following declaration has been made:
int i = 0, j = 3;
Using these starting values in each part, find the value of each variable
after the given statements have been executed.
(a) i = ++j + j; (b) i = j + j++;
(c) i = ++j*2 + ++j*3; (d) i = ++j*3 + ++j*2;
(e) j = 3*j-- + 2*j--; (f) j = 2*j-- + 3*--j;
- Rewrite as a single statement.
a) x++; (b) ++x;
y = x - z; z = x*y;
z++; --z;
- Rewrite as a sequence of three statements.
(a) m = n++ - p--; (b) p = ++n - m--;
- Find the values of j and k after execution of the following fragment.
j = 0;
k = 0;
j = --j;
k = k++;
| |