Break and continue jump out of loop in Javascritp

Source: Internet
Author: User

In javascript, the break statement is used to jump out of the loop page. continue is used to skip an iteration in the loop. It is actually a jump out of the loop, and the other enters the next loop. It is commonly used with while,, do while.

In JavaScript, use the break and continue statements to exit the loop:
 
• The role of the break statement is to jump out of the loop immediately, that is, not executing all subsequent loops;

Instance

The Code is as follows: Copy code

For (I = 0; I <10; I ++)
{
If (I = 3)
{
Break;
}
X = x + "The number is" + I + "<br> ";
}

• The function of the continue statement is to stop the loop being executed and directly enter the next loop.

Instance

The Code is as follows: Copy code

For (I = 0; I <= 10; I ++)
{
If (I = 3) continue;
X = x + "The number is" + I + "<br> ";
}


Comparison of break and continue statements:

The Code is as follows: Copy code

<Html>
<Head>
<Title> calculate the value of 1 + 2 + 3... + 98 + 99 + 100 </title>
</Head>
<Body>
<Script language = "JavaScript" type = "text/javascript">
Var total = 0;
For (var I = 1; I ++ ){
When (I> 100 ){
Break;
}
Total + = I;
Continue;
Alert (I );
}
Alert (total );
</Script>
</Body>
</Html>

Save and run the code. A warning box is displayed, showing 5050.

Analysis: after entering the loop, use the if statement to judge the value of I. if I> 100, run the break statement to end the loop. Otherwise, continue to execute it. When the continue statement is executed, the current loop is ended and the next loop is entered. alert (I) is not executed.

Related Article

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.