switch--------case
default总是放在最后,这时default后不需要break语句.并且,default部分也不是必须的,如果没有这一部分,当switch后面圆括号中表达式的值与所有case后面的常量表达式的值都不相等时,则不执行任何一个分支直接退出switch语句.此时,switch语句相当于一个空语句.例如,将上面例子中switch语句中的default部分去掉,则当输入的字符不是"A","B","C"或"D"时,此switch语句中的任何一条语句也不被执行.
1 function checkDay(){ 2 var now = new Date(); 3 var day = now.getDay(); 4 var week; 5 switch(day){ 6 case 1: 7 week = "星期一"; 8 break; 9 case 2: 10 week = "星期二"; 11 break; 12 case 3: 13 week = "星期三"; 14 break; 15 case 4: 16 week = "星期四"; 17 break; 18 case 5: 19 week = "星期五"; 20 break; 21 case 6: 22 week = "星期六"; 23 break; 24 case 7: 25 week = "星期日"; 26 break; 27 28 } 29 alert(week); 30 }
第二种情况
1 function checkDay(){ 2 var now = new Date(); 3 var day = now.getDay(); 4 var week; 5 switch(day){ 6 case 1: 7 case 2: 8 case 3: 9 week = "星期三"; 10 break; 11 case 4: 12 case 5: 13 case 6: 14 week = "星期六"; 15 break; 16 case 7: 17 week = "星期日"; 18 break; 19 20 } 21 alert(week); 22 }