The Javascript-break statement is used to jump out of loops

Source: Internet
Author: User



The break statement is used to jump out of a loop.

The Continue is used to skip an iteration in the loop.
Break statement

We have seen the break statement. It is used to jump out of a switch () statement.

The break statement can be used to jump out of a loop.

After the break statement jumps out of the loop, the code after the loop resumes (if any):
Instance

for (i=0;i<10;i++)
{
if (i==3)
{
Break
}
X=x + "The number is" + i + "<br>";
}

Try it yourself.

Because the IF statement has only one line of code, you can omit the curly braces:

for (i=0;i<10;i++)
{
if (i==3) break;
X=x + "The number is" + i + "<br>";
}

Continue statements

The continue statement interrupts the iteration in the loop, if the specified condition occurs, and then resumes the next iteration in the loop.

This example skips the value 3:
Instance

for (i=0;i<=10;i++)
{
if (i==3) continue;
X=x + "The number is" + i + "<br>";
}


JavaScript tags

As you saw in the switch statement chapter, you can tag JavaScript statements.

To mark a JavaScript statement, precede the statement with a colon:

Label
Statement

The break and continue statements are just statements that can jump out of a block of code.
Grammar

Break LabelName;

Continue labelname;

Continue statements (with or without label references) can only be used in loops.

The break statement (without a label reference) can only be used in loops or switch.

With a label reference, the break statement can be used to jump out of any JAVASCRIPT code block:
Instance

cars=["BMW", "Volvo", "Saab", "Ford";
List
{
document.write (Cars[0] + "<br>");
document.write (Cars[1] + "<br>");
document.write (cars[2] + "<br>");
Break list;
document.write (Cars[3] + "<br>");
document.write (Cars[4] + "<br>");
document.write (Cars[5] + "<br>");

}


+++++++++++++++++++++++++++++

<!--<! DOCTYPE html>
<body>
<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>

<p> Click the button to test the loop with the break statement. </p>
<button onclick= "myFunction ()" > click here </button>
<p id= "Demo" ></p>

<script>
function MyFunction ()
{
var x= "", i=0;
for (i=0;i<10;i++)
{
if (i==3)
{
Break
}
X=x + "The number is" + i + "<br>";
}
document.getElementById ("Demo"). Innerhtml=x;
}
</script>

</body>

-



<!--
<! DOCTYPE html>
<body>
<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>

<p> Click the button below to execute the loop, which skips the i=3 step. </p>
<button onclick= "myFunction ()" > click here </button>
<p id= "Demo" ></p>

<script>
function MyFunction ()
{
var x= "", i=0;
for (i=0;i<10;i++)
{
if (i==3)
{
Continue
}
X=x + "The number is" + i + "<br>";
}
document.getElementById ("Demo"). Innerhtml=x;
}
</script>

</body>
-


<! DOCTYPE html>
<body>

<script>
cars=["BMW", "Volvo", "Saab", "Ford";
List
{
document.write (Cars[0] + "<br>");
document.write (Cars[1] + "<br>");
document.write (cars[2] + "<br>");
Break list;
document.write (Cars[3] + "<br>");
document.write (Cars[4] + "<br>");
document.write (Cars[5] + "<br>");
}
</script>

</body>



The Javascript-break statement is used to jump out of loops

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.