下面的代码演示了JavaScript中do…while循环语句的使用方法
<!DOCTYPE html> <html> <body> <p>Click the button to loop through a block of as long as <em>i</em> is less than 5.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var x="",i=0; do { x=x + "The number is " + i + "<br>"; i++; } while (i<5) document.getElementById("demo").innerHTML=x; } </script> </body> </html>
输出结果如下
The number is 0 The number is 1 The number is 2 The number is 3 The number is 4