You are on page 1of 2

Increment and Decrement operators:

1. The operator which increases the value of a variable by one (1) is known as
increment operator(++).
2. The operator which decreases the value of a variable by one (1) is blown as
decrement operator(--).
Syntax (Increment operator)
• + + variable;
(This is known as Pre-Fix increment operation where value of the variable, first
increased by 1 then used.)
• variable + +;
(This is known as Post-Fix increment operation where the value of the
variable first used then increased by 1 and this new value will be used further
operations.)

Example1: Increment operator


(i) int d = 9;
System.outprintln( d + +); // step 1
System.out.println( + + d); //step2
Output:
9
11
Working: At step 1 the value of ‘d=9' will be printed then increase by 1, now d=10
will store in the memory. At step 2 due to ++d. the value of ‘d' will first increase
by 1 (d=11) which will now be printed (refer to above output).
(ii) int d= 9;
System.outprintln( + + d ); // step 1
System.out.println(d + +); // step 2
Output:
10
10
Working: At step 1 the value of ‘d' will first increase by 1 (d = 10) and then will
print. At step 2, due to d++, the previous value of ‘d=10' will be printed and then
increase by 1, now d = 11 will store in the memory for further use.
Syntax: (Decrement operator)
• --variable;
(This is known as Pre-Fix decrement operation where value of the variable,
first decreased by 1 then used.)
• variable --;
(This is known as Post-Fix decrement operation where the value of the variable
first used then decreased by 1 and this new value will be used further
operations.)
Example2: Decrement operator
(i) . int d = 9;
System.out.println( d - -); // step 1
System.out.println( - - d); // step 2
Output:
9
7
Working: At step 1 the value of ‘d=9' will first be printed then decrease by 1, now
d = 8 will store in the memory. At step 2 due to --d, the value of ‘d' will first
decrease by 1 (d = 7) which will now be printed (refer to above output).
(ii) . int d = 9;
System.out.println(- -d); // step 1
System.out.println(d- -); // step 2
Output:
8
8
Working:At step 1 the value of ‘d=9' first decrease by 1 (d=8) and then printed. At
step 2, due to d--, the previous value of ‘d=8'will be printed and then decrease by
1, now d = 7 will store in the memory for future use.

You might also like