亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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>點(diǎn)擊下面的按鈕,只要 i 小于 5 就一直循環(huán)代碼塊。</p>
<button onclick="myFunction()">點(diǎn)擊這里</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>點(diǎn)擊下面的按鈕,只要 i 小于 5 就一直循環(huán)代碼塊。</p>
<button onclick="myFunction()">點(diǎn)擊這里</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>點(diǎn)擊下面的按鈕,只要 i 小于 10 就一直循環(huán)代碼塊。</p> <button onclick="myFunction()">點(diǎn)擊這里</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