简体   繁体   English

for循环,仅终止表达式与while循环

[英]for loop with only terminating expression vs while loop

Whilst browsing the Apache ActiveMQ source code, i came across a funny looking for loop.. 浏览Apache ActiveMQ源代码时,我遇到了一个有趣的寻找循环。

 for (;beforeEndIndex < size;) {
     synchronizations.get(beforeEndIndex++).beforeEnd();
 }

Whats the benifit of this over using a standard while loop? 与使用标准while循环相比,这样做的好处是什么?

EG 例如

while(beforeEndIndex < size){
    beforeEndIndex++;
}

Both do exactly the same thing. 两者做的完全一样。

The major difference between a for loop and a while loop is that the for loop limits the scope of the iteration counter to within the for block where as a while loop requires you to declare the iteration counter at least one block higher. 一个之间的主要区别for回路和while循环是, for循环限制了迭代计数器的范围内for块,其中作为一个while循环需要你声明迭代计数器的至少一个块高。 In this case, as the for loop declares no iteration counter variable, there is no difference. 在这种情况下,因为for循环没有声明迭代计数器变量,所以没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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