When the user wants to execute a group of statements based on the value of Expression, the use of Switch Case
statements. Each value is called a "situation", and turned on in accordance with the test in each case variable. If the test expression and any user-specified Case
do not match, the implementation of Case Else
the statement.
Case Else
Is Select Case
one alternative statement, however, always use a Case Else
statement is a good programming practice.
grammar
The following is the VBScript Switch
syntax statements.
Select Case expression Case expressionlist1 statement1 statement2 .... .... statement1n Case expressionlist2 statement1 statement2 .... .... Case expressionlistn statement1 statement2 .... .... Case Else elsestatement1 elsestatement2 .... .... End Select
Examples
For demonstration purposes herein calculated by a function of type integer. Refer to the following diagram -
Reference Sample Code -
Private Sub switch_demo_Click() Dim MyVar As Integer MyVar = 1 Select Case MyVar Case 1 MsgBox "The Number is the Least Composite Number" Case 2 MsgBox "The Number is the only Even Prime Number" Case 3 MsgBox "The Number is the Least Odd Prime Number" Case Else MsgBox "Unknown Number" End Select End Sub
Performing the sample code above, the following results -