while ??
while (條件) { 需要執(zhí)行的代碼; }
while
for ??? ??? ?? ??? ? ??? ? ? ?? ?????. ??? ???? ?? for ??? ???? ??? ??? ?? ????? ?? ? ????. ? ?? while ??? ???? ?? ????.
while ???? ?? ??? ??? ????. ??? ???? ?? ??? ???? ??? ???? ??? ??? ?????. ?? ??, 100 ?? ?? ??? ?? ????? while ??? ???? ?? ??? ? ????.
var x = 0; var n = 99; while (n > 0) { x = x + n; n = n - 2; } x; // 2500
?? ??? ?? n? -1? ? ??? ?? ?????. while ??? ? ?? ???? ?? ??? ?????.
?
<!DOCTYPE html> <html> <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 + "The number is " + i + "<br>"; i++; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>rrree
??? ??? ??? do { ... } while() ??? while ??? ??? ???? ??? ???? ???? ????. ??, ??? ??? ??? ??? ?????:
do ... while
do { ... } while() ??? ??? ?? ?????. ?? ??? ??? ? ? ?????. for ? while ??? ? ?? ??? ? ????.
????
???