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!