简体   繁体   English

此条件在此while循环中如何工作?

[英]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 ? lenvar1吗?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM