Describes the usage of loop control statements in JavaScript, and details about javascript
JavaScript provides full control to process loops and switch statements. There may be a situation where you need to exit a loop but not reach its bottom. Alternatively, when you skip a part of the block and start the next iteration directly.
To handle these situations, JavaScript provides break and continue statements. These statements are used to immediately exit any loop or start the next iteration of the loop.
Break statement:
Break statement. This is a simple introduction using the switch statement. It is used to exit the loop in advance and break the closed curly braces.
Example:
This example shows how to use the break statement with the while loop. Note that the loop breaks the front and bottom of the document. write (...) statement from x to 5 at the beginning, and uses the right braces:
<script type="text/javascript"><!--var x = 1;document.write("Entering the loop<br /> ");while (x < 20){ if (x == 5){ break; // breaks out of loop completely } x = x + 1; document.write( x + "<br />");}document.write("Exiting the loop!<br /> ");//--></script>
This produces the following results:
Entering the loop2345Exiting the loop!
We can see that the break statement is used in the switch statement.
Continue statement:
The continue statement tells the interpreter to immediately start the next iteration of the loop and skip the remaining code blocks.
When a continue statement is encountered, the program process is immediately transferred to the loop check expression. If the conditions remain true, the next iteration is started; otherwise, the loop is exited.
Example:
This example shows that the use of the continue statement is the same as the while loop. Note that the continue statement is used to skip printing when the index variable x reaches 5:
<script type="text/javascript"><!--var x = 1;document.write("Entering the loop<br /> ");while (x < 10){ x = x + 1; if (x == 5){ continue; // skill rest of the loop body } document.write( x + "<br />");}document.write("Exiting the loop!<br /> ");//--></script>
This produces the following results:
Entering the loop234678910Exiting the loop!
Use tags to control the process:
Starting from JavaScript1.2, labels can be used with break and continue to control the process more accurately.
A label is a simple identifier that is subsequently applied to a statement or code block colon. We can see two different examples to learn more about the use of tags and continue.
Note: whether the linefeed is "continue" or "Break up declaration" is allowed between the tag names. In addition, there should be no other declaration between the label name and the associated loop.
Instance 1:
<script type="text/javascript"><!--document.write("Entering the loop!<br /> ");outerloop: // This is the label namefor (var i = 0; i < 5; i++){ document.write("Outerloop: " + i + "<br />"); innerloop: for (var j = 0; j < 5; j++) { if (j > 3 ) break ; // Quit the innermost loop if (i == 2) break innerloop; // Do the same thing if (i == 4) break outerloop; // Quit the outer loop document.write("Innerloop: " + j + " <br />"); }}document.write("Exiting the loop!<br /> ");//--></script>
This produces the following results:
Entering the loop!Outerloop: 0Innerloop: 0 Innerloop: 1 Innerloop: 2 Innerloop: 3 Outerloop: 1Innerloop: 0 Innerloop: 1 Innerloop: 2 Innerloop: 3 Outerloop: 2Outerloop: 3Innerloop: 0 Innerloop: 1 Innerloop: 2 Innerloop: 3 Outerloop: 4Exiting the loop!
Instance 2:
<script type="text/javascript"><!--document.write("Entering the loop!<br /> ");outerloop: // This is the label namefor (var i = 0; i < 3; i++){ document.write("Outerloop: " + i + "<br />"); for (var j = 0; j < 5; j++) { if (j == 3){ continue outerloop; } document.write("Innerloop: " + j + "<br />"); } }document.write("Exiting the loop!<br /> ");//--></script>
This produces the following results:
Entering the loop!Outerloop: 0Innerloop: 0Innerloop: 1Innerloop: 2Outerloop: 1Innerloop: 0Innerloop: 1Innerloop: 2Outerloop: 2Innerloop: 0Innerloop: 1Innerloop: 2Exiting the loop!