JavaScript语法入门系列(六) 循环语句

循环语句是指当条件为true时,反复执行某一个代码块的功能。JavaScript中有while, do…while,for,for..in四种循环语句。如果事先不确定需要执行多少次循环时一般使用while或者do…while循环,而确定使用多少次循环时一般使用for循环。for…in循环只对数组类型或者对象类型使用。

循环语句的代码块中也可以使用break语句来提前跳出循环,使用方法跟switch中相同。还可以用continue语句来提前跳出本次循环,进行下一次循环。

6.1 while语句

语法如下:

while(expression)statements                                                                              

它的执行顺序为:

1.   执行expression,如果结果为true,则执行statements

2.   继续重复执行步骤1。在statements或者expression中会改变expression值的代码,否则进入死循环状态。

例如:

var i=5;                                                                                                                 

var content=””;                                                                                                     

while(i>0)                                                                                                            

{                                                                                                                           

       content+=i;                                                                                                    

       i=i-1;                                                                                                              

}                                                                                                                           

document.write(content);                                                                                          

结果为网页显示54321。

注意:如果expression返回值一开始就是false时,statements一次也不会执行。

6.2 do...while语句

语法如下:

do{                                                                                                                       

statements                                                                                                           

}while(expression);                                                                                             

do…while语句在statements至少需要执行一次或者expression需要在statements执行过一次之后才能判断时使用。

 

例如:

var i=5;                                                                                                                 

var content=””;                                                                                                     

do                                                                                                                         

{                                                                                                                           

       content+=i;                                                                                                    

}while(i>10)                                                                                                         

document.write(content);                                                                                          

结果为网页显示5。

6.3 for语句

语法如下:

for(initialization;expression ;post-loop-expression) statements                                  

for语句的执行顺序是:

1.   先执行initialization初始化部分

2.   检查expression,如果为true则执行statements部分,如果为false则退出for语句

3.   执行post-loop-expression

4.   重复第二步和第三步知道expression为false。

例如:

var content=””;                                                                                                     

for(j=0;j<3;j++)                                                                                                      

{                                                                                                                            

       content+=j;                                                                                                   

}                                                                                                                            

document.write(content);                                                                                     

结果为网页显示012。

6.4 for...in语句

for…in循环语句主要应用在数组类型和对象类型的数据上。它自动遍历数据的每个元素。语法如下:

for(indexin object) statements                                                                             

它的执行顺序是对object包含的每个元素执行statements代码。注意:index变量为当前遍历到的元素的索引,而不是遍历到的元素。

例如:

var names = new Array(3)                                                                                    

names[0] = "John"                                                                                                

names[1]= "Andy"                                                                                                

names[2] = "Wendy"                                                                                             

for(x in names)                                                                                                     

{                                                                                                                            

       content+=names[x];                                                                                       

}                                                                                                                            

document.write(content);                                                                                      

结果为网页显示JohnAndyWendy

6.5 break语句

break语句用来提前结束整个循环。语法如下:

break;                                                                                                                   

       例如:

var nums = new Array(1,2,"error",4);                                                                   

for(x in nums)                                                                                                      

{                                                                                                                           

if(!isNaN(nums[x]))                                                                                             

{                                                                                                                           

    document.write(nums[x]);                                                                                    

}                                                                                                                           

else                                                                                                                             

{                                                                                                                           

    break;                                                                                                           

}                                                                                                                           

}                                                                                                                           

结果为网页输出内容“1.2.“。

6.6 continue语句

continue语句用来提前结束当前循环并且开始下一个循环。语法如下:

break;                                                                                                                   

       例如:

var nums = new Array(1,2,"error",4);                                                                   

for(x in nums)                                                                                                      

{                                                                                                                           

if(!isNaN(nums[x]))                                                                                             

{                                                                                                                           

    document.write(nums[x]);                                                                                    

}                                                                                                                           

else                                                                                                                             

{                                                                                                                           

    continue;                                                                                                      

}                                                                                                                           

document.write(“.”);                                                                                             

}                                                                                                                           

结果为网页输出内容“1.2.4.“。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值