How to use Break and Continue Statements in JavaScripts?
I have done the C and C++ programming languages. Now I have turned to the scripting languages few months before. I have started with the JavaScript. In that I am unable to use the break and continue statements.!! Can anybody explain me, how to use Break and Continue Statements in JavaScripts.?? Also Please provide me some sample of coding so that I can understand the concept more clearly.
Thanks in Advance..!!:notworthy
Re: How to use Break and Continue Statements in JavaScripts?
If you want the control over the code of the loop, you can use the break and continue statements. Because the break and continue statements provide stricter control over the execution of code in a loop. As the words suggests, the break statement will break the loop and continue executing the code that follows after the loop. The break statement exits the loop immediately and doing this prevents the further repetition of the code. On the other hand, continue statement exits the current repetition.
Re: How to use Break and Continue Statements in JavaScripts?
The following code demonstrates more about the break statements. I think that it will help you to understand. Here is an example for the break statement :
HTML Code:
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=15;i++)
{
if (i==5)
{
break;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
Re: How to use Break and Continue Statements in JavaScripts?
The following examples will let you know about the difference between the Break and the Continue Statements. First have look at the Break Statement :
Code:
var iNum = 0;
for (var i=1; i < 10; i++) {
if (i % 5 == 0) {
break;
}
iNum++;
}
alert(iNum); //outputs “4”
Now check the Continue Statement :
Code:
var iNum = 0;
for (var i=1; i < 10; i++) {
if (i % 5 == 0) {
continue;
}
iNum++;
}
alert(iNum); //outputs “8”
In Continue Statement, the alert displays “8” , the number of times the loop has been executed. when i reaches a value of 5, it returns to the top and the continue statement is executed, causing the loop to skip the expression iNum++.
Re: How to use Break and Continue Statements in JavaScripts?
Before looking at the combination of both the statements, I would like to recommend you to just have a look on single statements. Since, you have got the coding of the Break statement already, I am providing you with the Continue statement. The continue statement will break the current loop and continue with the next value. Here is the coding for that :
HTML Code:
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=15;i++)
{
if (i==5)
{
continue;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
Re: How to use Break and Continue Statements in JavaScripts?
In simple language, you can say that we use the break statement to terminate a loop, switch, or label statement. The syntax of the break statement is given below, it can either first or second :
If you use the first option, which is break without a label, it terminates the innermost enclosing while, do-while, for, or switch immediately and transfers control to the following statement. And if you use the second option, break with a label, it terminate the specified labeled statement.