0

In the below programs , post increment operator is used just after completion of expression evaluation. Now for the first program shouldn't the answer be 40 n then value of a be incremented to 41.N likewise for program 2 answer should be 41 instead of 42?

class IncrementDemo{
public static void main(String [] args){ 
    int a=20;
    a= a++ + a++;
    System.out.println(a); //Answer given is 41
}

class IncrementDemo{
public static void main(String [] args){ 
    int a=20;
    a= a++ + ++a; 
    System.out.println(a);
} 

Answer given is 42 for second program.

Michael
  • 41,989
  • 11
  • 82
  • 128
Joe
  • 43
  • 1
  • 3
  • 7

5 Answers5

3

You can understand the behaviour if you analyze how the statement is executed and the state of a in the meanwhile.

a = 20
a++ is executed, 20 is the result of the evaluation, a = 21
the second a++ is executed, 21 is the result, a = 22
the two results are added, 20 + 21 = 41, a = 41

on the other side, in the second case:

a = 20
a++ is executed, 20 is the result of the evaluation, a = 21
++a is executed, a = 22, 22 is the result
the two results are added, 20 + 22 = 42, a = 42

This is because the two increment operators are evaluated sequentially, so the second sees the effect of the first one.

bracco23
  • 2,181
  • 10
  • 28
0

The difference between those two is, that ++a will increment the value of a and return the incremented value. On the other hand a++ will increment the value, but return the original value that a had before being incremented.

naklar
  • 249
  • 1
  • 4
  • 7
0
a = a++ + a++

For the first a++, the value of a being used for the calculation is its actual value: 20, and after that it is incremented to 21. So, for the second a++, the value used for the calculation, is also the actual value of a, but this time it is 21 because it has been incremented in the first a++.

So you have 20 + 21, hence 41.

a = a++ + ++a

Same thing for a++, the actual value of a, 20 is used for the calculation, and then it is incremented to 21. Then ++a, the value is incremented before it is used, so it becomes 22, and then it is used for the calculation.

So you have 20 + 22, hence 42.

Pang
  • 9,564
  • 146
  • 81
  • 122
Asew
  • 374
  • 2
  • 13
0

The results are correct.

First case

int a=20;
a= a++ + a++;

This results in the following evaluation steps:

  • a = a++ + a++ (with a=20)
  • a = 20 + a++ (with a=21)
  • a = 20 + 21 (with a=22)

Second case

int a=20;
a= a++ + ++a; 

This results in the following evaluation steps:

  • a = a++ + ++a (with a=20)
  • a = 20 + ++a (with a=21)
  • a = 20 + 22 (with a=22)
mrnfrancesco
  • 1,017
  • 8
  • 26
0

When operator used in prefix mode, it increments the operand and evaluates to the incremented value of that operand. When used in postfix mode, it increments its operand, but evaluates to the value of that operand before it was incremented.

a= a++ + a++;

so first a++ will yield 20, now a value become 21

second a++ will yield 21, so final operation return 20+21 = 41

similar for second program

 int a=20;
 a= a++ + ++a; 

First a++ will yield 20, now a value become 21

second ++a will yield value 22

combining both will return result 20+22 = 42

mrnfrancesco
  • 1,017
  • 8
  • 26
Snehal Patel
  • 1,282
  • 2
  • 11
  • 25