问题描述
pre $
for(int i = 0; i< ; 10; i = i ++){
System.err.print(hoo ...);
$ b 这是永无止境的循环!
任何人都可以解释为什么会发生这种情况?
int i = 0; i
同样的:
$ b $ pre $ for(int i = 0; i
的第三部分部分>语句 - i = i ++
,计算方法如下: -
$ $ p $ int oldValue = i;
i = i + 1;
i = oldValue; //第三步
您需要删除作业从那里开始工作: - $ /
$ b $ pre $ for(int i = 0; i
(来自评论的OP请求)
x = 1; x = x ++ + x ++;
: -
就注释中指定的问题而言,以下表达式的结果为: -
x = 1;
x = x ++ + x ++;
获得如下: -
让我们来标记第二条语句的不同部分:
pre $ x $ c $ x = x ++ + x ++;
RAB
现在,首先RHS部分(A + B )
将被评估,然后最终结果将被分配给 x
。所以,让我们继续前进。
第一个 A
会被评估: -
old1 = x; //`old1变成1`
x = x + 1; //增加`x`。 `x变成2`
// x = old1; //这不会完成。由于价值尚未分配。
现在,由于将 A
分配给 R
在这里没有完成,第三步是不执行的。
现在,移动到 B
评估: -
old2 = x; // old2变成2.(因为`x`是2,从`A`的求值)
x = x + 1; //增加`x`。 `x变成3`。
// x = old2; //这将不会在这里完成。
现在,获取 x ++ + x ++
,我们需要在评估 A
和 B
时做最后一项任务,因为现在是该值被分配在 x
中。为此,我们需要替换: -
A - > old1
B - > old2 //两个评估的最后一个任务。 (A和B)
/ **参见分解`x = old1;`到最后,理解它如何等于`A = old1;在x = x ++`的情况下,考虑`x ++< ==> A`在这种情况下。 ** /
所以, x = x ++ + x ++ $ c $
x = old1 + old2;
= 1 + 2;
= 3; //因此答案是
第三部分 x = x ++
,看看它是如何工作的 x = x ++ + x ++
case: -
不知道为什么替换完成为 A - > old1
而不是 x - > old1
,例如 x = x ++
。
x = x ++
部分,特别是最后一项任务: - $ /
$ p $ x =属性oldValue;
如果您考虑 x ++
为 A
在这里,那么上面的赋值可以分解成以下步骤: -
A = oldValue;
x = A;
现在,对于目前的问题,它是一样的: - $ /
A = old1;
B = old2;
x = A + B;
我希望能够说清楚。
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?
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 3 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.
这篇关于增量运算符不会在for循环中递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!