Remember a little fun between operators

I came across a thread like this by chance

int i = 1;
int j = ++i + i++ + ++i + ++i + i++;
请问j最后等于多少?

At that time, the topic said that you don’t need to run the java editor, do the calculation yourself, and then copy it to the editor to see if the result is consistent. Out of curiosity, I calculated it by myself, and then it was inconsistent with the result given by the editor. I found that I was right for + + This operator is still unclear

analyze

According to the result analysis, ++i and i++ are divided into two steps when participating in the operation. One step is the operation itself of the + sign, and the other part is the i++ itself. The reason for the wrong result of the operation is that the step of ++i + i++ , I performed the ++ operation on the result after ++i + i, resulting in the result being larger than the actual result. The normal + operation is just the addition of i itself, and the + operation is over, and the subsequent i++ is just the operation of i itself It has nothing to do with the + operation, so the relationship will be smooth

Let the students perform calculation challenges by themselves!

Guess you like

Origin blog.csdn.net/qq_21875331/article/details/115066726