第七周学习周记——JavaScript学习

JavaScript学习…

1.for循环语句
for…in循环

我们可以使用for…in 语句去循环遍历对象的属性。

for…in语句的语法:

for (variable in object)
{
需要执行的代码块
}

每个属性都将执行一次需要执行的代码块。

2.while循环语句
只要当指定条件是真时,while循环语句会一直循环执行需要执行的代码块。

while循环的语法:

while (条件)
{
需要执行的代码块
}

例:

<!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
 <title></title>
 </head>
 <body> 
 <button type="button" onclick="myFunction()">我的心情可能是</button>
 <script>
function myFunction(){
	mood=["happy","delight","clam","upset","angry"];
	var l=mood.length, i=0;
    while (i<l){
		document.write(mood[i] + "<br>");
		i++;	
  }	
}
 </script>
 
 </body>
 

3.do…while语句
do…while语句是while循环语句的变体。与while语句不同的是,无论怎么样,do…while语句都会先执行一遍循环语句。在执行第一次循环后,do…while语句才会对条件进行判断,如果条件为真,那么语句将继续执行循环。如果条件为假,那么将退出循环。

do…while语句语法

do
{
需要执行的代码
}
while (条件);

例:

<!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
 <title></title>
 </head>
 <body> 
 <button type="button" onclick="myFunction()">我的心情可能是</button>
 <script>
function myFunction(){
	mood=["happy","delight","clam","upset","angry"];
	var l=mood.length, i=0;
    do{
		document.write(mood[i] + "<br>");
		i++;	
  }	
     while (i<l)
}
 </script>
 
 </body>

4.break语句
我们可以使用break语句跳出循环。当跳出循环后,会自动去执行循环后面的语句,而不是继续执行该循环中的下一个迭代。

例:

<!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
 <title></title>
 </head>
 <body> 
 <button type="button" onclick="myFunction()">我的心情可能是</button>
 <script>
function myFunction(){
	mood=["happy","delight","clam","upset","angry"];
    for (var i=0,l=mood.length; i<l; i++){
		if(i==2){
			break;
		}
		document.write(mood[i] + "<br>");
  }	
}
 </script>
 
 </body>
 </html>

执行结果是:

运行结果只显示出了i==2以前的执行结果,在i==2时跳出了循环。

5.continue语句
continue语句也可以用来跳出循环。但与break语句不同的是,continue语句跳出的是循环中的某一个迭代, 跳出后continue语句会将继续执行该循环中的下一个迭代。

例:

<!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
 <title></title>
 </head>
 <body> 
 <button type="button" onclick="myFunction()">我的心情可能是</button>
 <script>
function myFunction(){
	mood=["happy","delight","clam","upset","angry"];
    for (var i=0,l=mood.length; i<l; i++){
		if(i==2){
			continue;
		}
		document.write(mood[i] + "<br>");
  }	
}
 </script>
 
 </body>
 </html>

执行结果是:
在这里插入图片描述
运行结果只跳过出了i==2的循环迭代,即在i==2时跳出了迭代。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值