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

Javascript continue statement in while loop causes an infinite loop - Stack Overflow

programmeradmin3浏览0评论

I'm trying to create a while loop with a continue statement. However it seems to be causing an infinite loop and I can't figure out why.

The code below to me seems like it should start with the var tasksToDo at 3 then decrement down to 0 skipping number 2 on the way.

var tasksToDo = 3
while (tasksToDo > 0) {
    if (tasksToDo == 2) {
        continue;
    }
    console.log('there are ' + tasksToDo + ' tasks');
    tasksToDo--;
}

I'm trying to create a while loop with a continue statement. However it seems to be causing an infinite loop and I can't figure out why.

The code below to me seems like it should start with the var tasksToDo at 3 then decrement down to 0 skipping number 2 on the way.

var tasksToDo = 3
while (tasksToDo > 0) {
    if (tasksToDo == 2) {
        continue;
    }
    console.log('there are ' + tasksToDo + ' tasks');
    tasksToDo--;
}
Share edited May 7, 2017 at 23:09 ROMANIA_engineer 56.8k30 gold badges210 silver badges205 bronze badges asked Aug 23, 2013 at 12:55 moonshineOutlawmoonshineOutlaw 7232 gold badges9 silver badges21 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 3

conitnue, will go back to the while loop. and tasksToDo will never get decremented further than 2.

var tasksToDo = 3
while (tasksToDo > 0) {
if (tasksToDo == 2) {
 tasksToDo--;             // Should be here too.
 continue;
}

console.log('there are ' + tasksToDo + ' tasks');
tasksToDo--;
}

continue causes the loop to skip the decrement and begin all over again. Once tasksToDo hits 2, it stays 2 forever.

continue makes you go back to the beginning of the loop. You probably wanted to use break instead.

Or maybe make your decrement before the if block.

It's not very clear what you're doing but from what I understand, you're trying to avoid executing logic inside while for tasksToDo = 2

var tasksToDo = 3
while (tasksToDo > 0) {
    if (tasksToDo != 2) {
        console.log('there are ' + tasksToDo + ' tasks');
    }
    tasksToDo--;
}

It wouldn't make sense to add a break in case tasksToDo = 2 since it would be easier to add that condition to the while (tasksToDo > 2).

Code here might be totally different to your real code though so I could be missing something.

you are using continue; that continue your loop forever use break; to exit instead of continue;

Should it be like this?

var tasksToDo = 3
while (tasksToDo > 0) {
if (tasksToDo == 2) {
 continue;
 console.log('there are ' + tasksToDo + ' tasks');
 }
tasksToDo--;
}

The "continue;" statement prevent the execution of all remaining declarations in the code block.

Therefore the "tasksDo--" decrement is not executed after the loop reaches "i == 2" any more.

This creates an infinite loop!

use the "for" loop instead

the "for" loop solution for this case

var tasksToDo;

for (tasksToDo = 3; tasksToDo > 0; tasksToDo--){
    if (tasksToDo == 2) { continue; }
    console.log('there are ' + tasksToDo + ' tasks');
}

(the for loop accepts the decrement as its 3rd statement!)

发布评论

评论列表(0)

  1. 暂无评论