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

JavaScript while 循環(huán)

while 循環(huán)

while 循環(huán)會(huì)在指定條件為真時(shí)循環(huán)執(zhí)行代碼塊。

語(yǔ)法

while (條件)
  {
  需要執(zhí)行的代碼
  }

實(shí)例

本例中的循環(huán)將繼續(xù)運(yùn)行,只要變量 i 小于 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>

注意:如果您忘記增加條件中所用變量的值,該循環(huán)永遠(yuǎn)不會(huì)結(jié)束。這可能導(dǎo)致瀏覽器崩潰。    

運(yùn)行程序嘗試一下


do/while 循環(huán)

do/while 循環(huán)是 while 循環(huán)的變體。該循環(huán)會(huì)在檢查條件是否為真之前執(zhí)行一次代碼塊,然后如果條件為真的話(huà),就會(huì)重復(fù)這個(gè)循環(huán)。

語(yǔ)法

do
  {
  需要執(zhí)行的代碼
  }
while (條件);

實(shí)例

下面的例子使用 do/while 循環(huán)。該循環(huán)至少會(huì)執(zhí)行一次,即使條件為 false 它也會(huì)執(zhí)行一次,因?yàn)榇a塊會(huì)在條件被測(cè)試前執(zhí)行:

<!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>

運(yùn)行程序嘗試一下



Weiter lernen
||
<!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>
einreichenCode zurücksetzen