break 经常用来结束本次循环
实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="myFunction()">click</button>
<p id="demo"></p>
<script>
function myFunction(){
var text = "";
for (var i=0;i<5;i++){
if (i==3){
break;
}
text = text + "The Number is:" + i +"<br>";
}
document.getElementById("demo").innerHTML=text;
}
</script>
</body>
</html>
结果:
当满足条件时,break结束本次for循环。
continue 用来结束本次循环其中一次
实例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="myFunction()">click</button>
<p id="demo"></p>
<script>
function myFunction(){
var text = "";
for (var i=0;i<5;i++){
if (i==3){
continue;
}
text = text + "The Number is:" + i +"<br>";
}
document.getElementById("demo").innerHTML=text;
}
</script>
</body>
</html>
结果:
continue将i=3的此次循环结束了,后面代码不再执行,进入下一轮循环。
来源:CSDN
作者:梦里多梦
链接:https://blog.csdn.net/weixin_45801012/article/details/104697449