The break statement is used to jump out of a loop.
The Continue is used to skip an iteration in the loop.
Break statement
We have seen the break statement. It is used to jump out of a switch () statement.
The break statement can be used to jump out of a loop.
After the break statement jumps out of the loop, the code after the loop resumes (if any):
Instance
for (i=0;i<10;i++)
{
if (i==3)
{
Break
}
X=x + "The number is" + i + "<br>";
}
Try it yourself.
Because the IF statement has only one line of code, you can omit the curly braces:
for (i=0;i<10;i++)
{
if (i==3) break;
X=x + "The number is" + i + "<br>";
}
Continue statements
The continue statement interrupts the iteration in the loop, if the specified condition occurs, and then resumes the next iteration in the loop.
This example skips the value 3:
Instance
for (i=0;i<=10;i++)
{
if (i==3) continue;
X=x + "The number is" + i + "<br>";
}
JavaScript tags
As you saw in the switch statement chapter, you can tag JavaScript statements.
To mark a JavaScript statement, precede the statement with a colon:
Label
Statement
The break and continue statements are just statements that can jump out of a block of code.
Grammar
Break LabelName;
Continue labelname;
Continue statements (with or without label references) can only be used in loops.
The break statement (without a label reference) can only be used in loops or switch.
With a label reference, the break statement can be used to jump out of any JAVASCRIPT code block:
Instance
cars=["BMW", "Volvo", "Saab", "Ford";
List
{
document.write (Cars[0] + "<br>");
document.write (Cars[1] + "<br>");
document.write (cars[2] + "<br>");
Break list;
document.write (Cars[3] + "<br>");
document.write (Cars[4] + "<br>");
document.write (Cars[5] + "<br>");
}
+++++++++++++++++++++++++++++
<!--<! DOCTYPE html>
<body>
<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<p> Click the button to test the loop with the break statement. </p>
<button onclick= "myFunction ()" > click here </button>
<p id= "Demo" ></p>
<script>
function MyFunction ()
{
var x= "", i=0;
for (i=0;i<10;i++)
{
if (i==3)
{
Break
}
X=x + "The number is" + i + "<br>";
}
document.getElementById ("Demo"). Innerhtml=x;
}
</script>
</body>
-
<!--
<! DOCTYPE html>
<body>
<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<p> Click the button below to execute the loop, which skips the i=3 step. </p>
<button onclick= "myFunction ()" > click here </button>
<p id= "Demo" ></p>
<script>
function MyFunction ()
{
var x= "", i=0;
for (i=0;i<10;i++)
{
if (i==3)
{
Continue
}
X=x + "The number is" + i + "<br>";
}
document.getElementById ("Demo"). Innerhtml=x;
}
</script>
</body>
-
<! DOCTYPE html>
<body>
<script>
cars=["BMW", "Volvo", "Saab", "Ford";
List
{
document.write (Cars[0] + "<br>");
document.write (Cars[1] + "<br>");
document.write (cars[2] + "<br>");
Break list;
document.write (Cars[3] + "<br>");
document.write (Cars[4] + "<br>");
document.write (Cars[5] + "<br>");
}
</script>
</body>
The Javascript-break statement is used to jump out of loops