Tuesday, April 13, 2010

VB Scripting - Select Case

Previous11121314151617181920Next

Select Case statement in VB Scripting is also a branching statement. We branch out the code by evaluating one expression. Select Case is an alternative for If Else. Select Case can be used invariably (any where)



Syntax for Select Case Statement


Select Case [condition]
Case 1
aaaCode
Case 2
aaaCode
Case 3
aaaCode
Case Else
aaaCode
End Select


Example to understand it easily


Let us write a script which can display the resultant value of two integers based on the selection made. We are computing Addition, Subtraction, Multiplication and division of the two integers and based on the user selection the operation is performed.

UserInput = Inputbox("Enter any of the following options"&vbnewline&" " &vbnewline&"1 for Addition"&vbnewline&"2 for Subtraction"&vbnewline&"3 for Multiplication"&vbnewline&"4 for Division")

Var1 = CINT(Inputbox("Enter the First Value"))
Var2 = CINT(Inputbox("Enter the Second Value"))

Select Case UserInput

Case 1
aaamsgbox"The Sumation of "&Var1& " and " &var2& " is: "&Var1 + Var2
Case 2
aaamsgbox"The difference between "&Var1& " and " &var2& " is: "&Var1 - Var2
Case 3
aaamsgbox"The product of "&Var1& " and " &var2& " is: "&Var1 * Var2
Case 4
aaamsgbox"The quotient between "&Var1& " and " &var2& " is: "&Var1 \ Var2
Case Else
aaamsgbox "Invalid entry"
End Select


Previous11121314151617181920Next

No comments:

Post a Comment