Do While Loop Not Acting As I Expected

Do While Loop Not Acting As I Expected
0.0 0

#1

I was experimenting with a simple do while loop in JS, and it gave me unexpected results. I wonder if someone might explain why it does what it does?

Here’s the code:

var y = 0;

do {
    console.log(y);
    y -= 1;
}   while (y > 0);

My expectation was that on the first iteration, it would print out the value of y (namely, 0), decrement y, then notice that y is not greater than 0 (-1 is less than 0, as some elementary school teacher taught me long ago) and stop. So I should get 0 printed to my console and nothing else.

Actual results?

0
-1

Why? How is it getting to -1? And, having gotten to -1, why does it stop there?

All explanations appreciated!


#2

When I cut and paste that into codepen, it only outputs 0. Can you double check this?


#3

Is it possible that you have another console.log(y); later in your code?


#4

I got the same result as @ksjazzguitar in CodePen. To be clear: it should definitely not output -1.

When I tried repl.it, I did find the same behaviour as you. Not sure why it outputs the -1, but it doesn’t come from the do-while loop.


#5

@ksjazzguitar
@BenGitter

Sure. Here’s the exact code, including machine responses, copy-n-pasted directly from my console.

var y = 0;
undefined
do {
console.log(y);
y -= 1;
} while (y >0);
0
-1

That’s it, there isn’t any more.


#6

Maybe they are using node and it looks like the following which makes them think it is printing -1 when really it is just printing the 0; I believe node is just showing the last variable evaluated which would be y = -1?


#7

@rmdawson71, nice try, but no, it was plain-vanilla JS in the console. See my copy-n-paste above. (BTW, I’m a “he,” not a “they.”)


#8

@Jatudrei - I appologize, I should have said “the person” instead of “they”.
Then as far as why the -1 is showing, I have no idea.


#9

That seems to be it.

@Jatudrei, note that the console has the same behaviour, namely that it shows the last variable evaluated. Just check this by running x=0;, which will show 0 (at least in FF console).


#10

@rmdawson71, no problem. I’m not offended, I understand that you didn’t know from my username whether I’m a man or a woman and you didn’t want to assume. So I informed you–sorry if I came across as indignant; that was not my intention.

@BenGitter, yeah, I see that you’re right. I tried the same thing, but with console.log changed to alert, like thus:

do {
alert(y);
y -= 1;
} while (y >0);

If your explanation is right, I should get an alert window with the value “0” printed, and still see “-1” appear in the console.

Lo and behold, that’s exactly what happened.

Much thanks to everyone!


#11

BTW, I'm a "he," not a "they."

Good. We were concerned you might be a bunch of kids standing on each others’ shoulders in an overcoat trying to pass as an adult. That’s been happening a lot in fCC lately. But seriously…

Yeah, I get the same as you (Jatudrei) when I put it directly in the console. But notice that the -1 has a different prompt. Like someone suggested, I don’t think that’s output from the console.log but is just telling us the last status of the variable. I’m still new to typing directly into the console, so I’m guessing here.

 >  var y = 0;
 
     do {
        console.log(y);
        y -= 1;
     }   while (y > 0);
     0
 <- -1

This is my attempt to recreate what I’m seeing. Notice the extra prompt in front of the -1.


#12

Good eye, @ksjazzguitar! I completely overlooked that until you pointed it out. Thank you.