当用户希望根据表达式的值执行一组语句时,可以使用Select Case语句。每个值被称为一个案例,和可变被接通ON基于每种情况下。如果测试表达式与用户指定的任何Case不匹配,则执行Case Else语句。
Case Else是Select Case中的可选语句,但是,始终具有Case Else语句是一种良好的编程习惯。
语法
VBScript中的Select语句的语法:
Select Case expression Case expressionlist1 statement1 statement2 .... .... statement1n Case expressionlist2 statement1 statement2 .... .... Case expressionlistn statement1 statement2 .... .... Case Else elsestatement1 elsestatement2 .... .... End Select
例
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim MyVar MyVar = 1 Select case MyVar case 1 Document.write "The Number is the Least Composite Number" case 2 Document.write "The Number is the only Even Prime Number" case 3 Document.write "The Number is the Least Odd Prime Number" case else Document.write "Unknown Number" End select </script> </body> </html>
在上面的示例中,MyVar的值为1。因此,将执行情况1。
The Number is the Least Composite Number
作者:terry,如若转载,请注明出处:https://www.web176.com/vbscript/1297.html