国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

JavaScript Break and Continue statements

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中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>點擊按鈕,測試帶有 break 語句的循環(huán)。</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 + "該數(shù)字為 " + 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中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>點擊下面的按鈕來執(zhí)行循環(huán),該循環(huán)會跳過 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 + "該數(shù)字為 " + 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中文網(wǎng)(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



Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點擊按鈕,測試帶有 break 語句的循環(huán)。</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 + "該數(shù)字為 " + i + "<br>"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
submitReset Code