What does while (i --> 0) mean?
Asked Answered
R

5

12

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)

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?

Rhyolite answered 24/12, 2013 at 3:48 Comment(9)
It's i-- greater than 0Platitude
A spacing typo really got 4 upvotes?Fachan
@RUJordan - It's not a spacing typo. It's (probably) a deliberate attempt to form an arrow with --> as a kind of pseudo "count down" operator. The whole statement to be read as "i goes to zero"...Athanor
@Athanor Now that I know what it does, I agree that it's probably meant to just be humorous/annoying.Rhyolite
Just using while(i--) is enough.Staurolite
I didn't think of that. Kind of obfuscates the code a little bitFachan
@nderscore: That's a little different. In cases where you don't have a guarantee that i starts off >= 0, the loop could continue.Randolf
Don't worry, it's not a c++ pointer.Petrick
possible duplicate of What does the i--> opeator do in JavaScript?Earlie
D
14

It should be read as

i-- > 0

So, what really happens is,

  1. 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.

  2. Either way, the value of i will be decremented, immediately after the condition is checked.

Its always better to use for loop, when we run a loop with a counter, like this

for (var i = els.length - 1; i >= 0; i -= 1) {
    ...
}

Please read more about whether ++, -- is okay or not.

Danikadanila answered 24/12, 2013 at 3:49 Comment(5)
The value is checked on the value before the decrement. If it was written as --i > 0 then the value would be checked after the decrement.Namangan
@Cuberto Thats why I mentioned post decremented. :)Danikadanila
You did mention "post decremented", but you confused it by saying "and then the value will be checked", which makes it sound like that happens after decrementing...Athanor
So it's like i - 1 > 0?Petrick
@ManofSnow i - 1 will not actually change the value of i, i-- will change the value of i.Danikadanila
H
8

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

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
Hardbitten answered 24/12, 2013 at 3:50 Comment(3)
it's SO ;-) But anyway, your answer shared such a funny C++ piece of code which worth upvoting )Conde
lol... I just read the original answer and the comments followed it.Danikadanila
Yeah I think I read all of the answers and comments there at least onceHardbitten
P
1

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.

Pro answered 24/12, 2013 at 3:50 Comment(1)
The decrement occurs after comparison, --i is pre–decrement.Hema
T
1
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.

Tearful answered 6/5, 2022 at 0:5 Comment(0)
G
0

It's just weird spacing. It's same as

while (i--  >  0) {
Gutturalize answered 24/12, 2013 at 3:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.