# Do While Loop Not Acting As I Expected

**URL:** <https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763>\
**Category:** General\
**Created:** [April 6, 2017, 9:17pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763 "2017-04-06T21:17:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jatudrei](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jatudrei/32/29841_2.png) [@Jatudrei](https://forum.freecodecamp.org/u/Jatudrei)\
**Post date:** [April 6, 2017, 9:17pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/1 "2017-04-06T21:17:42Z")

</div>

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:

```auto
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?

```auto
0
-1

```

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

All explanations appreciated!

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [April 6, 2017, 9:23pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/2 "2017-04-06T21:23:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [April 6, 2017, 9:23pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/3 "2017-04-06T21:23:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [April 6, 2017, 9:25pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/4 "2017-04-06T21:25:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jatudrei](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jatudrei/32/29841_2.png) [@Jatudrei](https://forum.freecodecamp.org/u/Jatudrei)\
**Post date:** [April 6, 2017, 9:26pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/5 "2017-04-06T21:26:58Z")

</div>

@ksjazzguitar  
@BenGitter

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

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

```

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

---

<div class="post-metadata">

**Author:** ![Jatudrei](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jatudrei/32/29841_2.png) [@Jatudrei](https://forum.freecodecamp.org/u/Jatudrei)\
**Post date:** [April 6, 2017, 9:30pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/7 "2017-04-06T21:30:10Z")

</div>

@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.”)

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [April 6, 2017, 9:34pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/9 "2017-04-06T21:34:52Z")

</div>

> [@rmdawson71](#):
>
> I believe node is just showing the last variable evaluated which would be y = -1?

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

---

<div class="post-metadata">

**Author:** ![Jatudrei](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jatudrei/32/29841_2.png) [@Jatudrei](https://forum.freecodecamp.org/u/Jatudrei)\
**Post date:** [April 6, 2017, 9:38pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/10 "2017-04-06T21:38:48Z")

</div>

@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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [April 6, 2017, 9:39pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/11 "2017-04-06T21:39:11Z")

</div>

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

```auto
 > 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_.

---

<div class="post-metadata">

**Author:** ![Jatudrei](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jatudrei/32/29841_2.png) [@Jatudrei](https://forum.freecodecamp.org/u/Jatudrei)\
**Post date:** [April 6, 2017, 9:40pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/12 "2017-04-06T21:40:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:52pm UTC](https://forum.freecodecamp.org/t/do-while-loop-not-acting-as-i-expected/103763/13 "2021-01-17T20:52:05Z")

</div>

Closed on January 17, 2021, 8:52pm UTC


