JavaScript Break and Continue Statements

JavaScript Break and Continue Statements

The break StatementThe break statement will break the loop and continue executing the code that follows after the loop (if any).


var i=0;
for (i=0;i<=10;i++)
{
if (i==3)
{
break;
}
document.write("The number is " + i);
document.write("
");
}




output

The number is 0
The number is 1
The number is 2

The continue StatementThe continue statement will break the current loop and continue with the next value.



var i=0
for (i=0;i<=10;i++)
{
if (i==3)
{
continue;
}
document.write("The number is " + i);
document.write("
");
}




output

The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10

JavaScript For...In Statement
The for...in statement loops through the elements of an array or through the properties of an object.




var x;
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

for (x in mycars)
{
document.write(mycars[x] + "
");
}





output

Saab
Volvo
BMW

Read Users' Comments (0)

0 Response to "JavaScript Break and Continue Statements"

Post a Comment