Why pre-increment and post-increment act differently in C++ and java? [duplicate]

  c++, java, post-increment, pre-increment

For the same code java and c++ gives the different result

C++

int i, x;
i = 5;
x = ++i + i++;
cout << "x: " << x << std::endl;
cout << "i: " << i << std::endl;

Output
x: 13
i: 7

Java

int i, x;
i = 5;
x = ++i + i++;
System.out.println("x: " + x);
System.out.println("i: " + i);

Output
x: 12
i: 7

Why x’s value is different here? In c++ it is 13 but in java it is 12.

Source: Windows Questions C++

LEAVE A COMMENT