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

JavaScript while loop

while loop

The while loop loops through a block of code while a specified condition is true.

Syntax

while (condition)
{
Code to be executed
}

Example

The loop in this example will continue to run as long as variable i is less than 10:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>點擊下面的按鈕,只要 i 小于 5 就一直循環(huán)代碼塊。</p>
<button onclick="myFunction()">點擊這里</button>
<p id="demo"></p>
<script>
function myFunction(){
var x="",i=0;
while (i<5){
x=x + "該數(shù)字為 " + i + "<br>";
i++;
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>

Note : If you forget to increment the value of the variable used in the condition, the loop will never end. This may cause the browser to crash.

Run the program and try it


##do/while loop

The do/while loop is a variation of the while loop body. The loop executes the block of code once before checking whether the condition is true, and then repeats the loop if the condition is true.

Syntax

do

{
Code to be executed
}
while (condition );

Example

The following example uses a do/while loop. The loop will execute at least once, and it will execute even if the condition is false, because the code block will execute before the condition is tested:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>點擊下面的按鈕,只要 i 小于 5 就一直循環(huán)代碼塊。</p>
<button onclick="myFunction()">點擊這里</button>
<p id="demo"></p>
<script>
function myFunction(){
var x="",i=0;
do{
x=x + "該數(shù)字為 " + i + "<br>";
   i++;
}
while (i<5)  
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>

Run the program to try it



Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點擊下面的按鈕,只要 i 小于 10 就一直循環(huán)代碼塊。</p> <button onclick="myFunction()">點擊這里</button> <p id="demo"></p> <script> function myFunction(){ var x="",i=0; while (i<10){ x=x + "該數(shù)字為 " + i + "<br>"; i++; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
submitReset Code