The JavaScript-break statement is used to exit the loop.

Source: Internet
Author: User

The JavaScript-break statement is used to exit the loop.


The break statement is used to exit a loop.

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

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

The break statement can be used to exit a loop.

After the break statement jumps out of the loop, it will continue to execute the code after the loop (if any ):
Instance

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

Try it yourself

Since this if statement has only one line of code, curly braces can be omitted:

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

Continue statement

The continue statement interrupts the iteration in the loop. If a specified condition exists, it continues the next iteration in the loop.

In this example, the value 3 is skipped:
Instance

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


JavaScript tag

As you can see in the switch statement chapter, you can mark JavaScript statements.

To mark a JavaScript statement, add a colon Before the statement:

Label:
Statement

The break and continue statements are only statements that can jump out of the code block.
Syntax

Break labelname;

Continue labelname;

The continue Statement (with or without tag reference) can only be used in a loop.

The break statement (without tag reference) can only be used in loops or switches.

Through tag 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] +"
");
Document. write (cars [1] +"
");
Document. write (cars [2] +"
");
Break list;
Document. write (cars [3] +"
");
Document. write (cars [4] +"
");
Document. write (cars [5] +"
");

}

 

++












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






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.