Implementation of break forced-limit while,for loop in JavaScript

Source: Internet
Author: User

Break statement
Interrupts the current loop, or uses it with the label to break the associated statement.

break [Label];

The optional label parameter specifies the label of the statement at the breakpoint.

Description
Break statements are typically used in switch statements and while, for, for...in, or do...while loops. The most common is to use the label parameter in a switch statement, but it can be used in any statement, whether it is a simple statement or a compound statement.

Executing a break statement exits the current loop or statement and begins the execution of the statement immediately following the script

Apply break in For loop

<script language= ' JavaScript ' >
<!--
FORLOOP1:
for (var counter1 = 1; counter1 <= 5; counter1++)
{
for (var counter2 = 1; counter2 <= 5; counter2++)
{
document.write ("counter1=", counter1);
document.write ("counter2=", Counter2, "<BR>");
if (Counter2 = 3)
Break
if (Counter1 = 3)
Break ForLoop1;
}
}
document.write ("All done!");
-->
</SCRIPT>

In the While loop

<script language= "JavaScript" >
<!--
var x = 0;
while (x < 10)
{
x + +;
document.write (x);
Break
document.write ("Never seen!");
}
-->
</script>

<script language= "JavaScript" >
<!--
var x = 0, Breakat;
Breakat = Prompt ("Pick a number between 1", "" ");
while (x < 10) {
if (x = = Breakat)
Break
}
document.write (x);
x + +;
}
-->
</script>

There are two special statements that can be used within a loop: The break and Continue,break commands terminate the loop's operation, and then continue to execute the code after the loop (if there is code after the loop).

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.