JavaScript Break and Continue Statements

There are two special statements in JavaScript that can be used inside loops: break and continue.

Break Statement

We Use the break statement to terminate a loop, switch, or label statement.

When we use break without a label, it terminates the innermost enclosing while, do-while, for, or switch immediately and transfers control to the following statement.

When we use break with a label, it terminate the specified labeled statement.

The syntax of the break statement looks like this:

1. break;

2. break label;

Example

<html>
<body>
<script type=”text/javascript”>
var i=0;
for (i=0;i<=10;i++)
{
if (i==3)
{
break;
}
document.write(“The number is ” + i);
document.write(“<br />”);
}
</script>
</body>
</html>

The continue Statement

The continue statement will break the current loop and continue with the next value. When we use continue without a label, it terminates the current iteration of the innermost enclosing while, do-while or for statement and continues execution of the loop with the next iteration. In contrast to the break statement, continue does not terminates the execution of the loop entirely. In a while loop, it jumps back to the conditions. In a for loop, it jumps to the increment-expression.
When you use continue with a label, it applies to the looping statements identified with that label.
The syntax of the continue statement looks like the following:
1. continue
2. continue label

Example

<html>
<body>
<script type=”text/javascript”>
var i=0
for (i=0;i<=10;i++)
{
if (i==3)
{
continue;
}
document.write(“The number is ” + i);
document.write(“<br />”);
}
</script>
</body>
</html>
DOWNLOAD SOLUTION HERE