Java Reference
In-Depth Information
example,thefollowingexpressionsaddorsubtract1tothevalueoftheop-
erand.
x=x+1;
//add1tothevalue of x
y=y-1;
//subtract 1 from the value of y
The increment and decrement operators can be used to achieve the
same result in a more compact way, as follows:
x++;
// add 1 to the value of x
y--;
// subtract 1 from the value of y
Incidentally...
ThenameoftheC++ programminglanguageoriginatedinthenotion
of a version of the C language that was extended and improved. In
other words, C incremented, or C++.
The ++ and - - symbols can be placed before or after an expression.
Whenthesymbolsarebeforetheoperandtheoperatorissaidtobeinpre-
fix form. When it follows the operand it is said to be in postfix form . For
example:
z = ++x; // Prefix form
z = x++; // Postfix form
The prefix and postfix forms result in the same value in unary state-
ments. For example, the variable x is incremented by 1 in both of these
statements:
x++;
++x;
However, when the increment or decrement operators are used in an
assignment statement, the results are different if the operators are in pre-
fixorinpostfixform.Inthefirstcase(prefixform),theincrementordec-
rementisfirstappliedtotheoperandandtheresultassignedtothelvalue
of the expression. In the postfix form, the operand is first assigned to the
lvalue and then the increment or decrement is applied. The following
code fragment shows both cases.
intx=7;
int y;
y = ++x;
//y=8,x=8
y = x++;
//y=7,x=8
Search WWH ::




Custom Search