Almost all programming languages have break and continue statements, which shows their importance. If they are not important, they are practical enough. But how many people ignore their usage during the real development process? I believe that beginners or those who don’t care about program optimization should have a superficial understanding of it. This article strives to guide novices to re-understand break and continue statements through examples and detailed explanations for use.
Note: For the sake of demonstration, in this article, I choose my favorite JavaScript language. Other languages, such as C#, Java, Python, etc., are used the same way.
1. First read the help instructions of MSDN on break and continue statements
1. The break statement is used to terminate the most recent closed loop or the switch statement it is located in. Controls the statements passed to the termination statement (if any).
2. The continue statement passes control to the next iteration of the closed iteration statement where it is located.
2. My explanation of break and continue statements
Through our understanding of MSDN help, we can draw the following conclusions:
1. The break statement is used in statements with iterative selection characteristics such as loops (for, for in,...) and switch, and it terminates the nearest enclosed code block (that is, when multiple loops, it only terminates the loop it is in). The overall code continues to be executed after the break statement (if the break statement is not the last line of code).
2. The continuous statement is similar to the break statement. The continuous statement cannot be used in a separate switch statement, but can be used in a switch statement in a loop. Iterative statements (or loop statements) containing continue. After encountering a continue statement, the code will not execute in the regular code execution order from top to bottom, but will immediately return to the loop entrance and go to the next loop.
3. There are some differences when using break and continue statements in switch statements within a loop. break is to jump out of this switch, and the code behind the switch continues to be executed, while continue is to not execute the code after the switch, which can be understood as jumping out of the loop and then entering the next loop. Test the output of the following code using break and continue. If you use continue, you will find that after the program finds Microsoft, the document.write code will no longer be executed, and the output result will be one line less than using break.
The code copy is 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);
}
3. Use occasions for break and continue statements
1. Break can optimize the program and prevent the program from doing more useless work. In the following example, we want to find Microsoft companies from a huge list of companies. Once found, we will not continue to search. For example, whether the following statement is used to break or not is the same, but if the break statement is used, the program There are fewer steps to run unless the company you are looking for is at the end. The reason why I emphasize "large" here is to highlight the advantages of break. If there are too few, maybe you will think you can just use if statements.
The code copy is as follows:
var company=new Array('Adobe','Apple','Google','Intel','Microsoft','Oracle','IBM','SUN');
//Look for Microsoft from left to right (or from front to back) in the array company, and then find it and jump out of the loop through the break statement.
for (var i in company)
{
if (company[i]=='Microsoft')
{
document.write('Find Microsoft');
break;
}
}
Through single-step debugging of script debugging tools (such as Firebug plug-in for Firefox browsers), you can find that the break statement is used, and the loop is exited after five times. If you do not use the break statement, the loop needs to traverse the entire array.
2. The continue statement allows you to directly process these elements that meet the conditions in the process of traversing and finding elements that meet the conditions, without first finding the set of elements that meet the conditions, and then write another method outside to traverse these newly found elements again. And deal with it. Try comparing the following two implementation methods, you should understand the benefits of continue.
<1>Do not use the continue statement:
The code copy is 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:
The code copy is as follows:
//Demonstrate the usage of the continue statement. The following loop finds out and deletes 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];
}