JavaScript while循环

for循环是将计数器在for条件中声明。

while 循环是将计数器放到while循环之外声明。

 while 循环示例

 1 //计数器
 2 var i=9;
 3  
 4 /**while是while循环的关键字 ,
 5    *(i>=0)是循环条件
 6    *花括号里面的是循环体和计数器自减 */
 7 while(i>=0){
 8     println(i+' ');
 9     i--;
10 }
11  /**function是声明函数的关键字
12      * println是函数名称*/
13 function println(a) {
14     document.write(a);
15 }

示例声明

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>while循环</title>
 6 </head>
 7 <body>
 8 <script>
 9 var i=9;
10  
11 while(i>=0){
12     println(i+' ');
13     i--;
14 }
15  
16 function println(a) {
17     document.write(a);
18 }
19 
20 document.write('<br>'+"某四名学生的中考成绩:" );
21 
22 var array_cj =[640,480,913,298];
23 var j = 0;
24 while(j<array_cj.length){
25   println(array_cj[j]+'、');
26   j++;
27 }
28 </script>
29 </body>
30 </html>

猜你喜欢

转载自www.cnblogs.com/hzyhx/p/11133007.html