Increment and Decrement Operators


Increment and decrement operators are widely used in controlling loops. These operators provide a more convent and compact way of increasing or decreasing the value of a variable by one. For instance the statement x=x+1; can be simplified to x++; or ++x;
The increment operator increases the value of the variable by one while the decrement operator decreases the value of the variable by one. The value of the operand is altered to its new value. The operands required should be a variable. Constants or expressions are not allowed because of the purpose for which the operator is used. For instance, the expression, ++34; doesn’t make sense, as 34 isn’t a variable and cannot be increased by one. If you would wish to convince that 34 can be incremented to 35; it can but what we create is a new integer literal and we aren’t updating the 34 that is already present. Increment and decrement operators are used to update a variable to a new value.
Both these operators have two forms each: prefix and postfix. In prefix form, the operand appears before the operand while in post fix form, the operand appears after the operand. These operations are also referred to as pre increment, post increment, pre decrement and post decrement.
When used with prefix notation, the value of the variable referred to by the operand is first incremented and then the new value is used in the expression while in postfix notation, the value of the variable is first used in the expression and then increased. Look at the following examples:
int x = 34;
int y = ++x;
The value of x is first incremented to 34 and is then assigned to y. Therefore x is now 35 and y is also 35.
int x = 34;
int y = x++;
x is first assigned to y and then incremented by one. Therefore, x becomes 35 while y is assigned with the value 34.
Similarly, pre decrement and post decrement operators work.
The post fix or pre fix notations do not make a difference when the operators are applied as a statement by itself in the following way.
int s = 34;
s++; // s is now 35
int s= 34;
++s; // s is now 35
Give n below is an example to illustrate the use of increment and decrement operators:
int a = 3;
int b = a++;
int c = ++a;
int d = –b + ++c * –d – ++a / –d % ++c;
The first statement assigns 3 to the variable a. The second statement assigns the current value of a i.e. 3 to b and then increments a to 2. The third statement first increments a to 3 and then assigns its value i.e. 3 to c. Now, after the execution of the first three statements, the values of a, b and c are 3, 1 and 3 respectively.
Increment and decrement operators have a higher precedence than the other mathematical operators. Hence, they are applied first from left to right in the order in which they occur unless one of them has been given a higher precedence through the parantheses.
The following lines show the step by step application of the increment and decrement operators.
–b + ++c * b– – ++a / –b % c++

A = 3 b = 1 c = 3
0 + ++c * b– – ++a / –b % c++
a = 3 b = 0 c = 3

A = 3 b = 0 c = 3
0 + 4 * b– – ++a / –b % c++
a = 3 b = 0 c = 4

A = 3 b = 0 c = 4
0 + 4 * 0 – ++a / –b % c++
a = 3 b = -1 c = 4

A = 3 b = -1 c = 4
0 + 4 * 0 – 4 / –b % c++
a = 4 b = -1 c = 4

A = 4 b = -1 c = 4
0 + 4 * 0 – 4 / -2 % c++
a = 4 b = -2 c = 4

A = 4 b = -2 c = 4
0 + 4 * 0 – 4 / -2 % 4
a = 4 b = -2 c = 5

Now, the resulting expression is evaluated according to the rules of precedence of mathematical operators.
0 + ( 4 * 0 ) – 4 / -2 % 4
0 + 0 – ( 4 / -2 ) % 4
0 + 0 – ( -2 % 4 )
( 0 + 0 ) – 2
( 0 – 2 )
-2
Author: , 0000-00-00