2.3JS中的流程控制
基本和JAVA中的一致
顺序结构 略
分支结构 if switch
![](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)
![](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)
![](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
/*
* 定义变量表示月份
* 根据月份输出该月的季节和应季活动
* */
var i=10;
if(i ==12 || i ==1 || i==2){
alert("大约在冬季: 冬天吃火锅");
}else if(i >= 3 && i<=5 ){
alert("春天来了: 春天吃青菜");
}else if(i >= 6 && i <= 8){
alert("夏天到了: 吃烧烤喝啤酒");
}else if(i >= 9 && i <=11){
alert("秋天到了: 贴秋膘");
}else{
alert("月份有误")
}
switch(i){
case 3:
case 4:
case 5:
alert("春眠不觉晓");
break;
case 6:
case 7:
case 8:
alert("夏天蚊子咬");
break;
case 9:
case 10:
case 11:
alert("秋天一身包");
break;
case 1:
case 2:
case 12:
alert("冬天蚊子死翘翘");
break;
default:
alert("月份有误")
}
</script>
</head>
<body>
</body>
</html>
![](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)
循环结构 while do_while for
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
var i=1;
while(i<=10){
alert(i)
i++;
}
/*
* 考试,直到分数过线为止
* 求1-10整数和 1+10*(10/2) */
var sum=0;
var j=1;
do{
sum+=j;
j++;
}while(j<=10);
alert(sum)
var sum2=0;
for(var z=1;z<=10;z++){
sum2+=z;
}
alert(sum2)
</script>
</head>
<body>
</body>
</html>
![](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)
小练习:向页面打印99乘法表
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
/*
*
**
***
****
* */
for(var i =1;i<=9;i++){
for(var j=1;j<=i;j++){
document.write(j+"*"+i+"="+i*j+" ")
}
document.write("<br/>")
}
</script>
</head>
<body>
</body>
</html>
![](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202301/2531222-20230108212322937-397987789.gif)