JAVASCRITP break and continue jump out of circulation

Source: Internet
Author: User

In JavaScript, use break and continue statements to jump out of a loop:

The function of the break statement is to immediately jump out of the loop, that is, to no longer execute 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 executing loop and go directly to 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

<title> calculate the value of 1+2+3 ... +98+99+100 </title>
<body>
<script language= "JavaScript" type= "Text/javascript" >
var total=0;
for (var I=1; i++) {
if (i>100) {
Break
}
Total+=i;
Continue
alert (i);
}
Alert (total);
</script>
</body>

Save and run code, pop-up warning box, display 5050.

Analysis: After entering the loop, use the IF statement to determine the value of I, if i>100, execute the break statement, end the loop, or continue down execution. When executing to the Continue statement, the current loop is ended, the next loop is entered, and alert (i) is not executed.

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.