打卡时间:17:00
switch语句
switch(expression){
case value:statement
break;
}
switch语句中的每一种情形(case)的含义是:“如果表达式等于这个值(value),则执行后面的语句(statement)。”
类似于if···else语句
switch语句也有自己的特点
首先可以在switch语句中使用任何数据类型(在很多其他语言中只能使用数值),无论是字符串,还是对象都没有问题。其次,每个case的值不一定是常量,可以是变量,甚至是表达式。
例:
switch("hello world"){//字符串拼接的操作求值的表达式
case"hello"+"world":
alert("Greeting was found.");
break;
case"goodbye":
alert("closing was found.");
break;
default:
alert("unexpected message was found");
}
网友评论