Detailed explanation of the usage of circular control statements in JavaScript

Source: Internet
Author: User

This article mainly introduces the usage of circular control statements in JavaScript, including the use of break statements and continue statements, and the friends who need them can refer to the following

JavaScript provides full control to handle loops and switch statements. There may be a situation when you need to exit a loop but not reach its bottom. There may also be a situation when you want to skip over a portion of the code block and start the next iteration directly.

To handle these situations, JavaScript provides break and continue statements. These statements are the next iteration that is used to exit immediately from any loop or start loop.

Break statement:

Break statement, which is simply described with a switch statement, used to exit the loop prematurely, breaking the enclosing curly brace.

Example:

This example shows how to use the break statement with a while loop. Please note that the loop breaks the initial from X to 5,document.write (..) statement directly below the closing brace:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 <script type= "Text/javascript" > <!--var x = 1; document.write ("Entering the loop<br/>"); while (x < m) {if (x = = 5) {break;//breaks Out of loop completely} x = x + 1; document.write (x + "<br/>"); } document.write ("Exiting the loop!<br/>"); --> </script>

This will produce the following results:

?

1 2 3 4 5 6 Entering the Loop 2 3 4 5 exiting the loop!

We have seen the break statement used in the switch statement.

Continue statement:

The continue statement tells the interpreter to start the next iteration of the loop immediately and skips over the rest of the code block.

When the continue statement is encountered, the program flow is immediately transferred to the loop-checking expression, and if the condition is true, the next iteration is initiated, otherwise the control exits the loop.

Example:

This example shows the same while loop using the Continue statement. Note that the exponential variable x reaches 5 when the continue statement is used to skip printing:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 <script type= "Text/javascript" > <!--var x = 1; document.write ("Entering the loop<br/>"); while (X < ten) {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 will produce the following results:

?

1 2 3 4 5 6 7 8 9 10 Entering the Loop 2 3 4 6 7 8 9 exiting the loop!

Use labels to control processes:

Starting with JavaScript1.2, tags can be used with break and continue to continue to control the process more precisely.

The label is a simple identifier that is then applied to a statement or code block colon. See two different examples to learn about label usage breakthroughs and continue.

Note: Whether the newline character continues or is a parting statement is allowed between the label names. Also, there should be no other declaration between the label name and the associated loop.

Example 1:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <script type= "Text/javascript" > <!--document.write ("Entering the loop!<br/>"); Outerloop://This are the label name for (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 will produce the following results:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Entering the loop! outerloop:0 innerloop:0 innerloop:1 innerloop:2 innerloop:3 outerloop:1 innerloop:0 innerloop:1 innerloop:2 Inner Loop:3 outerloop:2 outerloop:3 innerloop:0 innerloop:1 innerloop:2 innerloop:3 outerloop:4 Exiting the loop!

Example 2:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 <script type= "Text/javascript" > <!--document.write ("Entering the loop!<br/>"); Outerloop://This are the label name for (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 will produce the following results:

?

1 2 3 4 5 6 7 8 9 entering the loop! outerloop:0 innerloop:0 innerloop:1 innerloop:2 outerloop:1 innerloop:0 innerloop:1 innerloop:2 outerloop:2 Inner loop:0 innerloop:1 Innerloop:2 exiting the loop!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.