最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

while loop to print out only odd numbers in javascript - Stack Overflow

programmeradmin11浏览0评论
let number = 0;

while (true) {
    if (number%2 === 0) continue;
    console.log(number);
    number ++;
}

I wanted to infinite loop to print out only odd numbers. But this doesn't seem to work. What am I doing wrong?

let number = 0;

while (true) {
    if (number%2 === 0) continue;
    console.log(number);
    number ++;
}

I wanted to infinite loop to print out only odd numbers. But this doesn't seem to work. What am I doing wrong?

Share Improve this question edited Feb 20, 2021 at 5:59 ChromeBrowser asked Feb 20, 2021 at 5:54 ChromeBrowserChromeBrowser 2293 silver badges7 bronze badges 5
  • 3 Run your program by-hand using only a pencil and grid-lined paper. You should see why immediately (if that doesn't work then I assume you don't understand how the continue statement works - in which case you should read this page: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Dai Commented Feb 20, 2021 at 5:54
  • 1 Q1: what is number%2? Q2: what is the condition for number to increase? Answer these questions two and a bug shall reveal itself to you. – VLAZ Commented Feb 20, 2021 at 5:57
  • 1 while(true) will block the main thread. In Node.js, this may work smoothly, but in browsers, this will mostly freeze the UI. – Sebastian Simon Commented Feb 20, 2021 at 6:06
  • Sebastian Simon, thanks dude but how e the answer at the bottom works and mine doesn't? They both use while (true) – ChromeBrowser Commented Feb 20, 2021 at 6:07
  • 4 Think about when number++ ever gets called... – Niet the Dark Absol Commented Feb 20, 2021 at 6:10
Add a ment  | 

5 Answers 5

Reset to default 1

Let's have a brief understanding of the code execution.

When the following code is executed...:

let number = 0;

while (true) {
    if (number%2 === 0) continue;
    console.log(number);
    number ++;
}

...the following happens:

  1. The first line allocates memory for a variable named number and stores a value of 0.
  2. The while loop starts, and it checks whether the condition is true, and yes it is, and always will be, as the condition is simply true.
  3. Then, it checks whether the number mod 2 returns 0, and definitely, it does, since 0÷2 is 0 and the remainder is 0, so the condition inside the if statement is true, and therefore it executes the code correspondingly. When it sees continue, it jumps back to step 2 and checks the while loop condition, then es back here, then goes back to step 2, then again es back here and this never ends(ends when you stop the code execution or close your browser obviously).

Simply put, it never goes forward to the console.log and the number++ line. This is why your code isn't working.

The code from others work because the control actually moves forward in the code and never actually bees stuck in the above loop.

Try this code

let number = 0;

while (true) {
    if (number % 2 != 0)
        console.log(number);
    number ++;
}

The code you're using will not work because you defined number=0 and then you're checking the condition if(number%2==0) continue; so this condition will always return true because the number is defined 0 and the number++ will never increase.

let number = 0;

while (true){ 
    if (++number % 2 == 1){
        console.log(number);
    }
}

no need a continue statement, just check the remainder is 1 the print the value in the loop. you can further shorten the if condition as if (++number % 2)

You should increment the number when it is even also, to check the next number and so on:

      let number = 0;

      while (true) {
         if (number%2 === 0){
            number++;
            continue;
         }
         console.log(number);
         number ++;
      }

Whatever number is, increment it first. Otherwise, when it is even, it never reaches to number++ any more.

let number = 0;

while (true) {
    number ++;
    if (number%2 === 0) continue;
    console.log(number);
}
发布评论

评论列表(0)

  1. 暂无评论