语法如下:
switch (expression) {
case label :
statementlist
case label :
statementlist
...
default :
statementlist
}
语句功能:当指定的表达式的值与某个标签匹配时,即执行相应的一个或多个语句。
语句实例:
一、单值实例
function MyObject() {
...}
switch (object.constructor){
case Date:
...
case Number:
...
case String:
...
case MyObject:
...
default:
...
}
function mtSubmitFindNearest(No)
{
var maptool = "FindNearest"
var ObjType, Range, ObjDes, Order, Other;
ObjType = "Name"
Range = ""
ObjDes = document.all.txtFindString1.value + ":all"
Order = 10
szFnObjName = "!!ObjectName==" + document.all.txtFindString2.value
if (szFnObjName == '输入关键字') szFnObjName= "";
switch ( No ) {
case 1:
Other = "ObjectLayers==" + slSearchLayer1.options[ slSearchLayer1.selectedIndex ].value + szFnObjName
break;
case 2:
Other = "ObjectName==" + document.all.txtFindString2.value
break;
default:
break;
}
mtSpellSubmit( maptool, ObjType, Range, ObjDes, Order, Other )
}
二、Case 语句可包含多个值和某个范围的值
Function bonus(ByVal performance As Integer, _ByVal salary As Decimal) As Decimal
Select performance
Case 1
Return salary * 0.1
Case 2,3
Return salary * 0.3
Case 3 To 7
Return salary * 0.7
Case 8 To 9
Return salary * 0.9
Case Is <= 15
Return salary * 1.2
Case Else
Return 0
End Select
End Function
每个 Case 语句可以包含一个值,比如:
Case 1
也可以包含多个值,多个值用逗号 (,) 分开,比如:
Case 2 Case 3
也可以是某个范围的值,比如:
Case 3 To 7
也可以是值和比较运算符的组合,比如:
Case Is <= 15