What is Select Case Statement in VBScript?
Select Case provides an alternate approach to nested If else loop. In case of multiple condition , it is better idea to read through the code and manage the expected behavior in the specific condition.
Syntax for Select Case Statement is :
SELECT CASE (operation)
CASE (expression 1)
line of Codes
CASE (expression 2)
line of Codes
CASE (expression 3)
line of Codes
.............
CASE (expression n)
line of Codes
CASE DEFAULT
line of Codes
END SELECT
How Select Case Works:
Let me Explain how select case works. consider the below problem statement to explain this ?
Let us take an example to perform expected action from sum, multiply, subtract, or divide and show the result as output from the function using Select case functionfunction Perform_arithmetic_Operation(Operation_Name, numA, numB)
Select Case operation_Name
Case "Addition", "Add"
Perform_arithmetic_Operation= numA+numB
Case "Subtraction", "Subtract"
Perform_arithmetic_Operation= numA+numB
msgbox "subtracting numB from numA gives: "& Perform_arithmetic_Operation
Case "Multiply"
Perform_arithmetic_Operation= numA*numB
Case "Divide"
Perform_arithmetic_Operation= numA/numB
case else
Perform_arithmetic_Operation = "Invalid Operation Name"
End Select
End function
This function when called will send the solution as expected based on operation_Name, and value of A and B.
example on calling above function as :
x = Perform_arithmetic_Operation("Addition", 13, 15)
msgbox x
will show value as 28 in the message box.
We can also provide multiple value in the expression. In above example ,we have provided multiple expression
e.g.: Case "Addition", "Add" . therefore the below expression will also add as in above example.
x = Perform_arithmetic_Operation("Add", 13, 15)
msgbox x
will show value as 28 in the message box.