2

I basically just want to print numbers a list of numbers a skip multiples of 3. I got it to work but the initial way i tried it did not work and i do not understand why, just need someone to please explain why it doesn't work and goes into an infinite loop.

This is the problem, why does it go into an infinite loop? I am clearly missing a key concept about code, if someone could help thanks.

var i = 0;
     while (i <= 10) {
         if (i % 3 == 0) {
            continue;
        }


       document.write( i + "</br>");
         i++;
        }

I know you can do it this way.

while (i <= 10) 
{

     if (i % 3 != 0) {

        document.write("Number is " + i + "<br />");  

    }

   i++

 }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Mathew
  • 75
  • 1
  • 4
  • 4
    Please try some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) of the first piece of code. Pay close attention to when and where you modify `i`. – Some programmer dude Jul 03 '19 at 06:45
  • I think this is answered here: https://stackoverflow.com/questions/18403444/javascript-continue-statement-in-while-loop-causes-an-infinite-loop – Jon B Jul 03 '19 at 06:51

2 Answers2

1

continue jumps to the next iteration and doesnt complete the rest of your code in the while. So i is not being incremented but rather staying as 0 becuase you wrote continue before incrementing the i. so therefore it is in an endless loop, it is always less than 10

L. Wolf
  • 86
  • 10
1

If we ignore the code producing the output and look only at the code checking and modifying i, it might become a little more clear why it's not working. It also helps to format our code for extra clarity.

var i = 0;

while (i <= 10) {
  if (i % 3 == 0) {
    continue;
  }

  i++;
}
  • Start with i = 0.
  • i <= 10 is true. Enter the loop.
  • i % 3 == 0 is true. Enter the if block.
  • continue;. Go straight to the top of the while loop again. Do not pass i++;. Do not collect 1.
  • Lather. Rinse. Repeat (infinitely).
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • Thank you, i assumed continue meant carry on to the next line and Break meant go back to the top of the loop? – Mathew Jul 03 '19 at 07:34
  • for(var i = 0; i <= 10; i ++){ if(i % 3 == 0){ continue; } document.write(i + ""); } – Mathew Jul 03 '19 at 07:37
  • in the situation above it dose not go back to the top of the for loop, it still does the document.write? so it still completes the loop, so why for a while loop does it not complete the loop? I am not trying to argue just trying to understand. Thank you – Mathew Jul 03 '19 at 07:38
  • @Mathew it _does_ go to the top of the loop, and lo and behold! There's an `i++` up there. [`break`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break) just exits the loop. – Patrick Roberts Jul 03 '19 at 07:43
  • @Mathew `continue` skips the rest of the current iteration and starts the next. `break` breaks out of the loop, effectively ending it. – Some programmer dude Jul 03 '19 at 07:52