10
9
8
7
6
5
4
3
2
1
0
ตัวอย่าง
var i=1
while(i<=10)
{
if (i % 5 == 0)
{
console.log("The first multiple of 5 between 1 and 10 is : "+i)
break //exit the loop if the first multiple is found
}
i++
}
ตัวอย่าง
var num=0
var count=0;
for(num=0;num<=20;num++)
{
if (num % 2==0)
{
continue
}
count++
}
console.log(" The count of odd values between 0 and 20 is: "+count)
outerloop: // This is the label name
for (var i = 0; i < 5; i++)
{
console.log("Outerloop: " + i);
innerloop:
for (var j = 0; j < 5; j++)
{
if (j > 3 ) break ; // Quit the innermost loop
if (i == 2) break innerloop; // Do the same thing
if (i == 4) break outerloop; // Quit the outer loop
console.log("Innerloop: " + j);
}
}
outerloop: // This is the label name
for (var i = 0; i < 3; i++)
{
console.log("Outerloop: " + i);
for (var j = 0; j < 5; j++)
{
if (j == 3){
continue outerloop;
}
console.log("Innerloop: " + j );
}
}