You have an infinite loop, because if numbers[i] > 70 it's not increasing i, so the next loop it's checking that same condition again without an incremented i value. There are multiple ways to fix this, but one option is to output the value when numbers[i] <= 70, and always increment i.
var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;
while (i < numbers.length){
if (numbers[i] <= 70){
console.log(numbers[i]);
}
i++;
}
Another option would be a different loop:
var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;
for(i = 0; i < numbers.length; i++) {
if (numbers[i] > 70){
continue;
}
console.log(numbers[i]);
}
Or a simpler way:
var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;
for(i = 0; i < numbers.length; i++) {
if (numbers[i] <= 70){
console.log(numbers[i]);
}
}
Answer from WOUNDEDStevenJones on Stack OverflowVideos
Why does continue cause an infinite loop in my while loop?
Can I use continue inside a forEach callback?
Does continue work in for...of and for...in loops?
You have an infinite loop, because if numbers[i] > 70 it's not increasing i, so the next loop it's checking that same condition again without an incremented i value. There are multiple ways to fix this, but one option is to output the value when numbers[i] <= 70, and always increment i.
var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;
while (i < numbers.length){
if (numbers[i] <= 70){
console.log(numbers[i]);
}
i++;
}
Another option would be a different loop:
var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;
for(i = 0; i < numbers.length; i++) {
if (numbers[i] > 70){
continue;
}
console.log(numbers[i]);
}
Or a simpler way:
var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;
for(i = 0; i < numbers.length; i++) {
if (numbers[i] <= 70){
console.log(numbers[i]);
}
}
You need to update i value before using continue if you don't the value of i will remain the same which leads to the loop becoming an infinite loop.
var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;
while (i < numbers.length){
if (numbers[i] >70){
i++;
continue;
}
console.log(numbers[i])
i++;
}
Check out this jsFiddle: http://jsfiddle.net/YdpJ2/3/
var getFalse = function() {
alert("Called getFalse!");
return false;
};
do {
continue;
alert("Past the continue? That's impossible.");
} while( getFalse() );
It appears to hit the continue, then break out of that iteration to run the check condition. Since the condition is false, it terminates.
continue does not skip the check while(false) but simply ignores the rest of the code within the brackets.