ArticleDirectory
- I. Let's take a look at msdn's help on break and continue statements.
- Ii. My interpretation of break and continue statements
- Iii. Use of break and continue statements
Almost allProgramThe language has break and continue statements, so we can see their importance. Even if it is not important, it is practical. But in the real development process, how many people will ignore their usage? I believe that beginners or those who do not care about program optimization should have a superficial understanding of it. This article attempts to guide cainiao to re-understand the break and continue statements through examples and usage cases.
Note: For ease of demonstration, this example selects my favorite JavaScript language. Other languages, such as C #, Java, and python, are the same usage.
I. Let's take a look at msdn's help on break and continue statements.
1. The break statement is used to terminate the recent closed loop or its switch statement. Control the statements passed to the end statement (if any ).
2. The continue statement passes control to the next iteration of its closed iteration statement.
Ii. My interpretation of break and continue statements
Through understanding the msdn help, we can draw the following conclusions:
1. The break statement is in the loop (for, for in ,......) It is used in statements with iterative selection features, such as switch, and is used to terminate the recent closure.CodeBlock (that is, when multiple loops exist, it only terminates its own loop), and the overall code continues to be executed 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. The continue statement cannot be used in a separate switch statement, but can be used in a switch statement in a loop. If an iteration statement containing a continue (or a loop statement) encounters a continue statement, the Code is not executed in the normal order from top to bottom, instead, it immediately returns to the loop entry and transfers it to the next loop.
3. When break and continue statements are used in switch statements in a loop, there are some differences. The break jumps out of the current switch, and the code after the switch continues to be executed. The continue is the code after the switch is not executed. It can be understood as jumping out of the loop and entering the next loop. Test the output of the following code using break and continue respectively. If you use continue, you will find that after the program finds Microsoft, document. if the write code is not executed, the output result is less than a line with 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 );
} Iii. Use of break and continue statements
1. Break can optimize the program and prevent it from being useless. In the following example, we need to find Microsoft companies from a large company list. Once we find them, we will not find them again. For example, the following statement does not use break, however, when the break statement is used, fewer steps are required to run the program, unless the company you are looking for is at the end. The reason why I emphasize the "huge" list here is to highlight the advantage of break. If it is too small, you may think you can use the if statement.
VaR Company = New Array ( ' Adobe ' , ' Apple ' , ' Google ' , ' Intel ' , ' Microsoft ' , ' Oracle ' , ' IBM ' , ' Sun ' );
// Search for Microsoft from the left to the right (or from the back) in the array company, find it, and use the break statement to jump out of the loop.
For ( VaR I In Company)
{
If (Company [I] = ' Microsoft ' )
{
Document. Write ( ' Find Microsoft ' );
Break ;
}
}
By using a single-step debugging tool (such as the firebug plug-in Firefox), you can find that the break statement is used, and the loop is exited after five cycles. If the break statement is not used, the entire array needs to be traversed in a loop.
2. The continue statement allows you to directly process these qualified elements while traversing and searching for qualified elements, instead of finding a qualified element set, then, the new elements are traversed and processed by another write method. Try to compare the following two implementation methods, 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:
// Demonstrate the continue statement usage. The following cycle finds out non-Microsoft and IBM company members and deletes them.
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];
}