The difference between break and continue in Java and usage analysis

  • 2020-04-01 02:52:30
  • OfStack

Almost all programming languages have break and continue statements, so they are important, even if they are not important enough to be useful. But how many people ignore their usage in the real development process? Believe that beginners or people who don't care about program optimization should have a superficial understanding of it. This article tries to guide the novice to recognize the break and continue statements again by examples and usage details.

Note: for the sake of illustration, I've chosen my favorite JavaScript language for the examples in this article. Other languages, such as C#, Java, Python, etc., are used in the same way.

First, look at the MSDN help for the break and continue statements

1. The break statement is used to terminate the most recent closed loop or the switch statement in which it resides. Control is passed to the statement following the termination statement, if any.

2. The continue statement passes control to the next iteration of the closed iteration statement it is in.

Ii. My interpretation of the break and continue statements

Based on the understanding of MSDN, we can draw the following conclusions:

1. The break statement is in a loop (for, for in,...). Used in statements with iterative selection characteristics, such as switch, that terminate the most recent closed block of code (that is, when multiple loops, it only terminates the loop it is in), 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 break statement in that the continue statement cannot be used in a separate switch statement, but can be used in a switch statement within a loop. An iterative statement containing a continue (or loop), upon encountering the continue statement, the code does not execute in the usual top-down order, but immediately returns to the loop entry and moves on to the next loop.

3. The break and continue statements are somewhat different when used in the switch statements within the loop. Break is the code after the switch to continue execution, while continue is the code after the switch is not executed, which can be understood as breaking out of the loop and then entering the next loop. Test the output of the following code using break and continue respectively. If you use continue, you will find that the document.write code will not execute after the program finds Microsoft, and the output will be one line less than if you use break.


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);
}

Use of break and continue statements

1. Break can optimize the program to prevent the program from doing more useless work. In the following example, we want to find Microsoft from a large list of companies. Once we find it, we won't go further. For example, the following statement will have the same effect if we do not use break, but with the break statement, the program will run in fewer steps, unless the company we are looking for is at the end. The reason I'm emphasizing the "big" list here is to give you the break advantage, and if it's too small, you might think you could just use an if statement.


var company=new Array('Adobe','Apple','Google','Intel','Microsoft','Oracle','IBM','SUN');
//Look for Microsoft from left to right (or front to back) in array company, find it, and then break out of the loop with a break statement.
for (var i in company)
{
    if (company[i]=='Microsoft')
    {
        document.write('Find Microsoft');
        break;
    }
}

Step by step with a script debugging tool, such as the Firebug plug-in for the Firefox browser, you can see that you use the break statement, loop through it five times, and then exit the loop. Without the break statement, the loop traverses the entire array.

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201401/20140119154259.jpg" >

2. The continue statement allows you to process elements that meet the criteria directly while walking through and looking for elements that meet the criteria, rather than having to find a set of elements that meet the criteria and then write a new method outside to iterate over the newly found elements. Try to contrast the two implementations below, and you should understand the benefits of continue.

< 1 > Do not use the continue statement:


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:


//To demonstrate the use of the continue statement, the following loop identifies and removes 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 articles: