class Codechef {
public static void main (String[] args)
{ int k=1;
System.out.println(++k + k++ + + k);
}
}
I think the steps are like this ( increment postfix has precedence over
prefix):
1. k++ returns 1 to use, then increments k, so k now is 2.
++k + 1 + + k; // k is 2
2.unary operator && prefix have same precedence ,so associativity is from
right to left...so
++k + 1 + 2;
3 ++k increments k and uses that value (++2) returning 3 3 + 1 + 2; //k
is 3
4.now, go left to right: so, 3 + 1 + 2 = 6
BUT, according to the book, the expression is "parsed as ( (++k) +
(k++) ) + (+k) which yields 2 + 2 + 3 = 7