The vbscript is executed line by line until the end of the script and this order of execution is called flow. The execution flow can be controlled by certain commands or statements in vbscript. Following are the conditional statements which are used to control the order of execution.
- If … Then … Else
- Select Case
IF .. Then.. Else:
Syntax:
If … (condition) Then
… Set of Statements ….
Else
— Set of Statements ….
EndIf
If the condition is true then the set of statements before else gets executed otherwise statements after the else gets executed. Let us try to understand this with an example.
X=inputbox("Input value for X") Y=inputbox("Input value for Y") If (X>Y) Then msgbox "Value of X is greater than Y" ElseIf (Y>X) Then msgbox "Value of Y is greater than X" Else msgbox "X and Y are equal" End If
Type this code into Notepad and then save it as “ifelse.vbs” and then double click the file. Inputbox will ask you to input two values for X & Y. In the If statement the condition X>Y / X<Y will be evaluated and if it is true then the msgbox under the if statement gets executed otherwise msgbox under else gets executed.
Select Case
Syntax:
Select Case Variable
Case Variable
….Set of Statements…
Case Variable
….Set of Statements…
Case Else
….Set of Statements…
End Select
Using this conditional statement we can choose across a set of values. If a variable can have five different values and we need to execute a set of statements for each of these values, then we can go ahead and use Select Case. We can definitely use even If Then Else but code looks cluttered.
Example:
X=cint(Inputbox("Enter value for X")) Y=cint(Inputbox("Enter value for Y")) op=Inputbox("What operation to be done?... ADD, SUBTRACT,MULTIPLY,DIVIDE") Select Case op Case "ADD" sum=X+Y msgbox "summation is : "&sum Case "SUBTRACT" st=X-Y msgbox "subtraction x-y : "&st Case "MULTIPLY" mult=X*Y msgbox "multiplication x*y : "&mult Case "DIVIDE" dvd=X/Y msgbox "division x/y : "&dvd Case Else msgbox "None of the arithmetic operations match.." End Select
In the above example we need to choose one of the arithmetic operations among four (ADD, SUBTRACT, MULTIPLY, and DIVIDE). Using accomplish this task using Select Case. If none of the case statement matches the criteria then the Case Else gets executed and displays the message as “None of the arithmetic operations match…”
hey this script has helped me a lot it is easy to understand