[英]What does while (i --> 0) mean?
I apologize if this a stupid question, but I cannot find the answer anywhere.如果这是一个愚蠢的问题,我深表歉意,但我无法在任何地方找到答案。
How does the following code work?以下代码如何工作? (I realize that it loops over the elements of els
) (我意识到它循环遍历els
的元素)
var i = els.length;
while (i --> 0) {
var el = els[i];
// ...do stuff...
}
I have no idea what -->
means.我不知道-->
是什么意思。 There is no documentation for it.没有它的文档。 Can someone enlighten me?有人可以启发我吗?
It should be read as 它应该被读为
i-- > 0
So, what really happens is, 所以,真正发生的是
value of i
will be checked if it greater than 0, if it is true then control will enter the while
block, if it is false while
block will be skipped. 将检查i
值是否大于0,如果为true,则控制将进入while
块;如果为false while
则将跳过while
块。
Either way, the value of i
will be decremented, immediately after the condition is checked. 无论哪种方式,价值i
将递减,病情检查之后 。
Its always better to use for
loop, when we run a loop with a counter, like this 当我们使用计数器运行循环时,最好总是使用for
循环
for (var i = els.length - 1; i >= 0; i -= 1) {
...
}
Please read more about whether ++
, --
is okay or not. 请详细了解++
, --
是否可以。
It's just weird spacing, should be 这只是奇怪的间距,应该是
while((i--) > 0)
it's just post-decrementing and checking the condition. 它只是递减并检查条件。 There was this humorous answer at the C++ question, but I think it got deleted 在C ++问题上有一个幽默的答案,但我认为它已被删除
while (x --\
\
\
\
> 0) //i goes down to zero!
Or something like that, anyway 还是这样的
So if you had something like 所以如果你有类似的东西
var i=3;
while(i-->0){
console.log(i);
}
it would return 它会回来
2
1
0
It's just weird spacing. 这只是奇怪的间隔。 It's same as 一样
while (i-- > 0) {
The code should actually be: 该代码实际上应该是:
while (i-- > 0) {
where the loop will run if the value after the variable i
has been decremented is greater than zero. 如果变量i
减后的值大于零,则循环将在其中运行。
while (i--> 0)
{
// ...do stuff
}
is same as与
while (i>0)
{
i--;
// ...do stuff
}
IMHO we should write simple code rather than clever code because it's not understandable by everyone.恕我直言,我们应该编写简单的代码而不是聪明的代码,因为它不是每个人都能理解的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.