慕课前端入门-js流程控制语句

1. if分支

if(条件1){
      ...
}else if(条件2){
      ...
}else{
}

##      2. switch
```#js
switch(expression){
      case value:
            statement;
            break;
      case value:
            statement;
            break;
      default:
            statement;
}
<script type="text/javascript">
	var week = new Date().getDay();
	var weekstr;
	switch(week){
		case 0:
			weekstr="日";
			break;
		case 1:
			weekstr="一";
			break;
		case 2:
			weekstr="二";
			break;
		case 3:
			weekstr="三";
			break;
		case 4:
			weekstr="四";
			break;
		case 5:
			weekstr="五";
			break;
		case 6:
			weekstr="六";
			break;
		default:
			weekstr="...系统错误,你是怎么走到这一步的";
			break;
	}
	document.write("今天是星期"+weekstr);
</script>

3.for循环

for(var i=0;i<100;i++){
      console.log(i);
}

4.while循环

先判断,后执行:while(){}
先执行,后判断:do{}while()

<script type="text/javascript">
var i=0;
while(i<10){
	console.log(i);
	i++;
}
var j=0;
do{
    console.log(j);
    j++;
}while(j<5)
</script>

5. break和continue

break:立即退出循环
continue:结束本次循环,继续开始下一次

posted on 2020-09-11 13:15  singleSpace  阅读(153)  评论(0编辑  收藏  举报