In javascript, the break statement is used to jump out of the loop page. continue is used to skip an iteration in the loop. It is actually a jump out of the loop, and the other enters the next loop. It is commonly used with while,, do while.
In JavaScript, use the break and continue statements to exit the loop:
• The role of the break statement is to jump out of the loop immediately, that is, not executing all subsequent loops;
Instance
The Code is as follows: |
Copy code |
For (I = 0; I <10; I ++) { If (I = 3) { Break; } X = x + "The number is" + I + "<br> "; } |
• The function of the continue statement is to stop the loop being executed and directly enter the next loop.
Instance
The Code is as follows: |
Copy code |
For (I = 0; I <= 10; I ++) { If (I = 3) continue; X = x + "The number is" + I + "<br> "; } |
Comparison of break and continue statements:
The Code is as follows: |
Copy code |
<Html> <Head> <Title> calculate the value of 1 + 2 + 3... + 98 + 99 + 100 </title> </Head> <Body> <Script language = "JavaScript" type = "text/javascript"> Var total = 0; For (var I = 1; I ++ ){ When (I> 100 ){ Break; } Total + = I; Continue; Alert (I ); } Alert (total ); </Script> </Body> </Html> |
Save and run the code. A warning box is displayed, showing 5050.
Analysis: after entering the loop, use the if statement to judge the value of I. if I> 100, run the break statement to end the loop. Otherwise, continue to execute it. When the continue statement is executed, the current loop is ended and the next loop is entered. alert (I) is not executed.