[英]How does this condition work in this while-loop?
while ((len = var1) != -1)
{
// do something
}
Which is being compared to -1? 哪个与-1比较?
len
or var1
? len
或var1
吗?
This is Java EE. 这是Java EE。
The return value of an assignment var = val
is the assigned value val
. 赋值
var = val
的返回值是赋值val
。 So what happens here is that len
is assigned with the value of var1
, and if (after the assignment) its value is not equal to -1
, the loop is entered. 因此,这里发生的是为
len
分配了var1
的值,如果(在分配之后)其值不等于-1
,则进入循环。
The most technical answer, I suppose, would be that you are comparing len
to -1, but that doesn't really capture what's going on. 我想,最技术性的答案是,您正在将
len
与-1进行比较,但这并不能真正捕获正在发生的情况。
What's really going on is that, for every check, you are setting len
to var1
, and then comparing len
. 真正的实际情况是,对于每次检查,您都将
len
设置为var1
,然后将len
进行比较。 So, if I'm not mistaken, even though the computer is actually comparing len
, it will always have just been replaced with the value from var1
, so the code will be functionally identical to 因此,即使我没有记错,即使计算机实际上在比较
len
,它也总是会被var1
的值替换,因此代码在功能上与
len = var1;
while(var1 != -1)
{
len = var1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.