Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

java - Post increment operator not incrementing in for loop

I'm doing some research about Java and find this very confusing:

for (int i = 0; i < 10; i = i++) {
  System.err.print("hoo... ");
}

This is never ending loop!

Anybody has good explanation why such thing happens?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
for (int i = 0; i < 10; i = i++) {

The above loop is essentially the same as: -

for (int i = 0; i < 10; i = i) {

the 3rd part of your for statement - i = i++, is evaluated as: -

int oldValue = i; 
i = i + 1;
i = oldValue;  // 3rd Step 

You need to remove the assignment from there, to make it work: -

for (int i = 0; i < 10; i++) {

(On OP request from Comments)

Behaviour of x = 1; x = x++ + x++; : -

As far as your issue as specified in the comment is concerned, the result of the following expression: -

x = 1; 
x = x++ + x++;

is obtained as follows: -

Let's mark different parts of the second statement: -

x = x++ + x++;
R    A     B

Now, first the RHS part (A + B) will be evaluated, and then the final result will be assignmed to x. So, let's move ahead.

First A is evaluated: -

old1 = x;  // `old1 becomes 1`
x = x + 1; // Increment `x`. `x becomes 2`
//x = old1; // This will not be done. As the value has not been assigned back yet.

Now, since the assignment of A to R is not done here, the 3rd step is not performed.

Now, move to B evaluation: -

old2 = x;  // old2 becomes 2. (Since `x` is 2, from the evaluation of `A`)
x = x + 1; // increment `x`. `x becomes 3`.
// x = old2; // This will again not be done here.

Now, to get the value of x++ + x++, we need to do the last assignment that we left in the evaluation of A and B, because now is the value being assigned in x. For that, we need to replace: -

A --> old1
B --> old2   // The last assignment of both the evaluation. (A and B)

/** See Break up `x = old1;` towards the end, to understand how it's equivalent to `A = old1; in case of `x = x++`, considering `x++ <==> A` in this case. **/

So, x = x++ + x++, becomes: -

x = old1 + old2;
  = 1 + 2;
  = 3;  // Hence the answer

Break up of 3rd part of x = x++, to see how it works in x = x++ + x++ case: -

Wonder why the replacement is done as A --> old1 and not x --> old1, as in case of x = x++.

Take a deep look at x = x++ part, specially the last assignment: -

x = oldValue;

if you consider x++ to be A here, then the above assignment can be broken into these steps: -

A = oldValue;
x = A;

Now, for the current problem, it is same as: -

A = old1;
B = old2;
x = A + B;

I hope that makes it clear.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...