ECMAScript break and continue Statements
- Previous Page break Statement
- Next Page with Statement
The break and continue statements provide more strict control over the execution of code within loops.
The difference between break and continue statements
The break statement can immediately exit the loop, preventing any further execution of code.
The continue statement only exits the current loop and allows the next loop to continue according to the control expression.
For example:
var iNum = 0; for (var i=1; i<10; i++) { if (i % 5 == 0) { break; } iNum++; } alert(iNum); // Output "4"
In the above code, the for loop iterates the variable i from 1 to 10. In the loop body, the if statement checks whether the value of i can be evenly divided by 5 (using the modulus operator). If it can be evenly divided, the break statement will be executed. alert displays "4", which is the number of times the loop is executed before exiting.
If the continue statement replaces the break statement in this example, the result will be different:
var iNum = 0; for (var i=1; i<10; i++) { if (i % 5 == 0) { continue; } iNum++; } alert(iNum); // Output "8"
Here, alert will display "8", which is the number of times the loop is executed. The total number of possible executions of the loop may be 9, but when the value of i is 5, the continue statement will be executed, causing the loop to skip the expression iNum++ and return to the beginning of the loop.
Used with labeled statements
Both break statement and continue statement can be used in conjunction with labeled statements to return to a specific position in the code.
Usually, when there is a loop inside a loop, it is done like this, for example:
var iNum = 0; outermost: for (var i=0; i<10; i++) { for (var j=0; j<10; j++) { if (i == 5 && j == 5) { break outermost; } iNum++; } } alert(iNum); //Output "55"
In the above example, the label outermost represents the first for statement. Normally, each for statement executes 10 times of code block, which means that iNum++ should normally be executed 100 times, and at the end, iNum should be equal to 100. Here, the break statement has a parameter, which is the label of the statement to jump to after the loop stops. In this way, the break statement can not only jump out of the inner for statement (i.e., the statement using the variable j), but also jump out of the outer for statement (i.e., the statement using the variable i). Therefore, the final value of iNum is 55 because when both i and j are equal to 5, the loop will terminate.
The continue statement can be used in the same way:
var iNum = 0; outermost: for (var i=0; i<10; i++) { for (var j=0; j<10; j++) { if (i == 5 && j == 5) { continue outermost; } iNum++; } } alert(iNum); //Output "95"
In the above example, the continue statement forces the loop to continue, not only the inner loop, but also the outer loop. When j is equal to 5, this means that the inner loop will reduce 5 iterations, resulting in the value of iNum being 95.
Tip:It can be seen that labeled statements used in conjunction with break and continue are very powerful, but excessive use of them can cause trouble in debugging code. Make sure that the labels used are descriptive and do not nest too many layers of loops.
Tip:To learn what a labeled statement is, please read ECMAScript Labelled Statements This Section.
- Previous Page break Statement
- Next Page with Statement