0
int a = 6;
int b  =  a + a++; 
answer is 12

int a = 6;
int  c  = a++ + a; 
answer is 13

why are the answers for b and c are different ? since postfix has higher a precedence value, then the answer for both should be 13 right?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
Harry
  • 11
  • 2
  • Look here: https://introcs.cs.princeton.edu/java/11precedence/. Note that postfix and prefix operators both of higher precedence than "+" (like you said).. Note, too, that postfix is left to right (hence "7 + 6"), like you'd expect... but prefix is right to left (hence 6 + 6, then increment). – paulsm4 Mar 25 '23 at 06:01

1 Answers1

0

In the first example, you incremented the value of a in the second operand, so it doesn't affect the value of the first operand. But, in the second example, you incremented the value of a in the first operand, so it affects the value of the second operand.

Sharofiddin
  • 340
  • 2
  • 14