# Question about do...while loop

**URL:** <https://discourse.mozilla.org/t/question-about-do-while-loop/108889>\
**Category:** Learn\
**Created:** [December 15, 2022, 6:54pm UTC](https://discourse.mozilla.org/t/question-about-do-while-loop/108889 "2022-12-15T18:54:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![church\_craig](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/church_craig/32/59416_2.png) [@church\_craig](https://discourse.mozilla.org/u/church_craig)\
**Post date:** [December 15, 2022, 6:54pm UTC](https://discourse.mozilla.org/t/question-about-do-while-loop/108889/1 "2022-12-15T18:54:20Z")

</div>

My code seems to be causing an infinite loop and I can’t figure out why. Here it is:

```auto
const input = document.querySelector('input');

const btn = document.querySelector('button');

const para = document.querySelector('p');

btn.addEventListener('click', () => {
  para.textContent = 'Output: ';
  const num = input.value;
  input.value = '';
  input.focus();
  let i = 1;
  do {
    let sqRoot = Math.sqrt(i);
    if(Math.floor(sqRoot) !== sqRoot) {
      continue;
    }
    para.textContent += `${i} `;
    i++;
  } while(i <= num);
}); 

```

Why would this code cause an infinite loop?

---

<div class="post-metadata">

**Author:** ![mikoMK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mikomk/32/50456_2.png) [@mikoMK](https://discourse.mozilla.org/u/mikoMK)\
**Post date:** [December 16, 2022, 9:31am UTC](https://discourse.mozilla.org/t/question-about-do-while-loop/108889/3 "2022-12-16T09:31:30Z")

</div>

Hi @church_craig

The problem is the use of `continue`. It stops the current round of the loop and starts the next one. In this case `i++` won’t get called and the loop’s next round will have the same value for `i`.  
A solution would be to chance the condition to `Math.floor(sqRoot) === sqRoot` and add the text inside the `if` statement. This will always call `i++` and if we have a square number it will also output it.

See you,  
Michael

---

<div class="post-metadata">

**Author:** ![church\_craig](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/church_craig/32/59416_2.png) [@church\_craig](https://discourse.mozilla.org/u/church_craig)\
**Post date:** [December 16, 2022, 5:41pm UTC](https://discourse.mozilla.org/t/question-about-do-while-loop/108889/4 "2022-12-16T17:41:05Z")

</div>

This post is very bizarre.

What does it mean?

---

<div class="post-metadata">

**Author:** ![church\_craig](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/church_craig/32/59416_2.png) [@church\_craig](https://discourse.mozilla.org/u/church_craig)\
**Post date:** [December 16, 2022, 6:44pm UTC](https://discourse.mozilla.org/t/question-about-do-while-loop/108889/5 "2022-12-16T18:44:37Z")

</div>

I tried what you suggested and it worked. As you may be aware I took the code from the Looping code article on MDN and tried to apply a `do...while` loop to it. The original code is used to display the use of the `continue` statement. Here is the original code:

```auto
const para = document.querySelector('p');
const input = document.querySelector('input');
const btn = document.querySelector('button');

btn.addEventListener('click', () => {
  para.textContent = 'Output: ';
  const num = input.value;
  input.value = '';
  input.focus();
  for (let i = 1; i <= num; i++) {
    let sqRoot = Math.sqrt(i);
    if (Math.floor(sqRoot) !== sqRoot) {
      continue;
    }
    para.textContent += `${i} `;
  }
});

```

The article states that the `for`, `while` and `do...while` loops are largely interchangeable. So my question is can the code be rewritten with a `do while` loop **and** include `if(Math.floor(sqRoot) !== sqRoot) {continue}`, or do you have to remove the `continue` statement and rewrite the code with `if(Math.floor(sqRoot) === sqRoot)`?

---

<div class="post-metadata">

**Author:** ![mikoMK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mikomk/32/50456_2.png) [@mikoMK](https://discourse.mozilla.org/u/mikoMK)\
**Post date:** [December 18, 2022, 8:11pm UTC](https://discourse.mozilla.org/t/question-about-do-while-loop/108889/6 "2022-12-18T20:11:43Z")

</div>

I think the intention behind “largely interchangeable” is to say “with small changes”. Using my changes from above seems to be the clearest change.  
That being said, if you want to keep the `if` statement the same, you could do the following:

- Change the initial value of `i` to `0`
- Move `i++` to the start of the loop
- Change the loop condition to `lesser than`

```auto
btn.addEventListener('click', () => {
  para.textContent = 'Output: ';
  const num = input.value;
  input.value = '';
  input.focus();
  let i = 0;
  do {
    i++;
    let sqRoot = Math.sqrt(i);
    if(Math.floor(sqRoot) !== sqRoot) {
      continue;
    }
    para.textContent += `${i} `;
    
  } while(i < num);
});

```
