Switch Statements in VBScript

Advertisements


When a User want to execute a group of statements depending upon a value of an Expression, then Switch Case is used. Each value is called a Case, and the variable being switched ON based on each case. Case Else statement is executed if test expression doesn't match any of the Case specified by the user.

Case Else is an optional statement within Select Case, however, it is a good programming practice to always have a Case Else statement.

Syntax :

The syntax of a Switch Statement in VBScript is:

Select Case expression
   Case expressionlist1
      statement1
      statement2
      ....
      ....
      statement1n
   Case expressionlist2
      statement1
      statement2
      ....
      ....
   Case expressionlistn
      statement1
      statement2
      ....
      ....   
  Case Else
      elsestatement1
      elsestatement2
      ....
      ....
End Select

Example :

<!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>

In the above example, the value of MyVar is 1. Hence, Case 1 would be executed.

The Number is the Least Composite Number 


Advertisements
Advertisements