java vs C++ in pre and post increment

The results are different because the lauguages are specified differently.

why java says 50

Because the order of evaluation is defined so in Java. The addition is: 6+7+8+9+10+10 == 50. The final post increment is overwritten by the assignment and has no effect.

while c++ says 51

The shown program has undefined behaviour in C++, so it could have any output. It happened to be 51 in this case. More details in this answer: https://stackoverflow.com/a/4176333/2079303

Leave a Comment