break statement is used to break out of the loop.
continue Used to skip an iteration in the loop.
Break Statement
We have already seen the break statement in the previous chapters of this tutorial. It is used to break out of switch() statements.
The break statement can be used to break out of a loop.
After the break statement breaks out of the loop, the code after the loop will continue to be executed (if any):
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>点击按钮,测试带有 break 语句的循环。</p> <button onclick="myFunction()">点击这里</button> <p id="demo"></p> <script> function myFunction(){ var x="",i=0; for (i=0;i<10;i++){ if (i==3){ break; } x=x + "该数字为 " + i + "<br>"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
Run the program and try it
Continue statement
continue statement interrupts the iteration in the loop if the specified condition occurs , and then continue with the next iteration in the loop. This example skips value 3:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>点击下面的按钮来执行循环,该循环会跳过 i=3 的步进。</p> <button onclick="myFunction()">点击这里</button> <p id="demo"></p> <script> function myFunction(){ var x="",i=0; for (i=0;i<10;i++){ if (i==3){ continue; } x=x + "该数字为 " + i + "<br>"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
Run the program to try it
JavaScript tag
As you As you saw in the chapter on switch statements, JavaScript statements can be marked.
To label a JavaScript statement, add a colon before the statement:
label:
statements
The break and continue statements are just Statements that can jump out of code blocks.
Syntax:
break labelname;
continue labelname;
The continue statement (with or without label reference) can only be used within a loop.
break statement (without label reference), can only be used in a loop or switch.
Referenced through tags, the break statement can be used to break out of any JavaScript code block:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> direction=["上","下","左","右"]; list:{ document.write(direction[0] + "<br>"); document.write(direction[1] + "<br>"); document.write(direction[2] + "<br>"); break list; document.write(direction[3] + "<br>"); document.write(direction[4] + "<br>"); document.write(direction[5] + "<br>"); } </script> </body> </html>
Run the program to try it out