Information Technology Reference
In-Depth Information
Table 8-10. Behavior of Pre- and Post- Increment and Decrement Operators
Expression:
x = 10
Value Returned
to the Expression
Value of Variable
After Evaluation
Pre-increment
++x
11
11
x++
Post-increment
10
11
Pre-decrement
--x
9
9
x--
Post-decrement
10
9
For example, the following is a simple demonstration of the four different versions of the
operators. In order to show the different results on the same input, the value of the operand x
is reset to 5 before each assignment statement.
int x = 5, y;
y = x++; // result: y: 5, x: 6
Console.WriteLine("y: {0}, x: {1}", y, x);
x = 5;
y = ++x; // result: y: 6, x: 6
Console.WriteLine("y: {0}, x: {1}", y, x);
x = 5;
y = x--; // result: y: 5, x: 4
Console.WriteLine("y: {0}, x: {1}", y, x);
x = 5;
y = --x; // result: y: 4, x: 4
Console.WriteLine("y: {0}, x: {1}", y, x);
This code produces the following output:
y: 5, x: 6
y: 6, x: 6
y: 5, x: 4
y: 4, x: 4
Search WWH ::




Custom Search