JavaScript Break and Continue Statements!!!

Posted on 20/09/2009 by

0



Break and Continue are two special statements that can be used inside Loops.

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

The Break Statement

Example:

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

And the Result is:
The number is 0
The number is 1
The number is 2



The Continue Statement will break the current Loop and continue with the next value.

The Continue Statement

Example:

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

And the Result is:
The number is o
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

EllaHax

Advertisements
Posted in: Coding, JavaScript