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

JavaScript while 循環(huán)

while 迴圈

while 迴圈會(huì)在指定條件為真時(shí)循環(huán)執(zhí)行程式碼區(qū)塊。

語(yǔ)法

while (條件)
? {
? 需要執(zhí)行的程式碼
? }

實(shí)例

本例中的迴圈將繼續(xù)運(yùn)行,只要變數(shù)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>

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

執(zhí)行程式嘗試


do/while 迴圈

do/while 迴圈是while 迴圈的變體。這個(gè)迴圈會(huì)在檢查條件是否為真之前執(zhí)行一次程式碼區(qū)塊,然後如果條件為真的話,就會(huì)重複這個(gè)迴圈。

語(yǔ)法

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

實(shí)例

下面的範(fàn)例使用do/while 迴圈。這個(gè)迴圈至少會(huì)執(zhí)行一次,即使條件為false 它也會(huì)執(zhí)行一次,因?yàn)槌淌酱a區(qū)塊會(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>

執(zhí)行程式嘗試



繼續(xù)學(xué)習(xí)
||
<!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>
提交重置程式碼