Difference between break and continue in Java and analysis of the use situation _java

Source: Internet
Author: User
Tags terminates
Almost all programming languages have break and continue statements, which can be seen as important, even if it's unimportant. But in the real development process, how many people will ignore their usage? People who believe in beginners or who don't care about program optimization should be more superficial about it. This paper tries to guide the rookie to recognize the break and continue statements by example and use the occasion.

Note: For the sake of demonstration, this example chooses my favorite JavaScript language, other languages, such as C #, Java, Python, etc. are the same usage.

First, look at the MSDN help Note on break and continue statements

1, the break statement is used to terminate the most recent enclosing loop or the switch statement in which it resides. Controls the statements that are passed to the end statement (if any).

2, the Continue statement passes control to the next iteration of the enclosing iteration statement in which it resides.

Ii. my interpretation of break and continue statements

With an understanding of MSDN help, we can draw the following conclusions:

1, break statements in the loop (for, to in, ...) and switch, such as a statement with an iterative selection feature, that terminates the most recent enclosing code block (that is, it only terminates its own loop), and the overall code continues execution after the break statement (if the break statement is not the last line of code).

2, the Continue statement is similar to the use of the break statement, the continue statement is not available in a separate switch statement, but can be used in a switch statement within a loop. An iterative statement (or loop) that contains continue, which, after encountering the continue statement, does not execute sequentially from the code that is normally executed from the top down, but immediately goes back to the loop to the next loop.

3, break and continue statements are used in a switch statement within a loop, which is somewhat different. The break is the code that jumps out of the switch,switch, and continue is not executing the switch code, it can be understood to jump out of the loop, and then go to the next loop. Test the following code using the break and continue two cases of output, if you use continue, you will find that the program found Microsoft, the document.write code does not execute, the output is less than using the break line.

Copy Code code as follows:

var company=new Array (' Adobe ', ' Apple ', ' Google ', ' Intel ', ' Microsoft ', ' Oracle ', ' IBM ', ' SUN ');
for (var i in company)
{
Switch (Company[i])
{
Case ' Microsoft ':
Continue
Break
}
document.write (' Me was run ' +i);
}

Iii. use of break and continue statements

1, break can optimize the program, do not allow the program to do more without effort. In the following example, we're going to find a Microsoft company from a huge list of companies, and once we find it, it's not going to go down again, like the following statement with no break is the same effect, but with the break statement, the program runs less steps, unless the company is looking for the last. My emphasis here on the "big" list is to highlight the break advantage, too little, and perhaps you'll think you can use the IF statement.

Copy Code code as follows:

var company=new Array (' Adobe ', ' Apple ', ' Google ', ' Intel ', ' Microsoft ', ' Oracle ', ' IBM ', ' SUN ');

Look for Microsoft in the array company from left to right (or in the past), and then jump out of the loop with the break statement.
for (var i in company)
{
if (company[i]== ' Microsoft ')
{
document.write (' Find Microsoft ');
Break
}
}

Through the Script debugging tool (such as Firefox browser's Firebug plug-in) one step debugging can discover, uses the break statement, loops five times then exits the loop. Instead of using the break statement, loops iterate through the full array.



2. The Continue statement allows you to directly process these qualifying elements directly while traversing and finding the qualifying elements without first finding the set of elements that match the criteria, and then writing the method again outside to iterate through the newly found elements and do the processing. Try to compare the following two implementation methods, you should understand the benefits of continue.

<1> do not use continue statements:

Copy Code code as follows:

var company=new Array (' Adobe ', ' Apple ', ' Google ', ' Intel ', ' Microsoft ', ' Oracle ', ' IBM ', ' SUN ');
var findcompany=[];
for (var i in company)
{
if (company[i]== ' Microsoft ' | | | company[i]== ' IBM ')
{
Findcompany.push (Company[i]);
}
}
for (var i in Findcompany)
{
Delete Findcompany[i];
}

<2> Use the Continue statement:

Copy Code code as follows:

Demonstrates the use of continue statements, the following loops to remove from non-Microsoft and IBM Company members.
var company=new Array (' Adobe ', ' Apple ', ' Google ', ' Intel ', ' Microsoft ', ' Oracle ', ' IBM ', ' SUN ');
for (var i in company)
{
if (company[i]== ' Microsoft ' | | | company[i]== ' IBM ')
{
Continue
}
Delete Company[i];
}

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.