for循环是将计数器在for条件中声明。while do循环是将计数器放到while循环之外声明。
While循环
示
1
2
3
4
5
6
7
8
9
10
|
var i=0 while (i<10){ println(i); i++; } function println(a) { document.write(a + '<br>' ); } |
示例说明
打印数字9-0
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>while循环</title> 6 </head> 7 <body> 8 <script> 9 var i=9; 10 while(i>=0){ 11 print(i); 12 i--; 13 } 14 15 function print(a){ 16 document.write(a+'<br>'); 17 } 18 19 </script> 20 </body> 21 </html>