Javascript Break And Continue
Break and Continue statements are used inside a loop to control the flow of execution.
Break Statement
The break statement is placed inside the loop to terminate the iteration and start to execute the code immediately after the loop.
Example
<script>
for(var i = 1; i< 10; i++) {
if(i == 5)
{
break;
}
document.write("Number : " + i + "<br/>");
}
document.write("Loop terminates");
</script>
Output:
Continue Statement
Continue statement is used inside a loop. When continue statement is executed, it stops the current iteration of the loop and continue with the next iteration of the loop.
Example
In the given example, the loop skips to print the statement inside 'document.write()' and continues from the for loop terminates at value of variable i equals to 5.
<script>
for(var i = 1; i< 10; i++) {
if(i == 5)
{
continue;
}
document.write("Number : " + i + "<br/>");
}
</script>