VBScript – Flow Control with Conditional Statements

by seetaram on August 26, 2011

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.

  1. If … Then … Else
  2. Select Case

IF .. Then.. Else:


If … (condition) Then

… Set of Statements ….


— Set of Statements ….


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


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.


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"
                     msgbox "summation is : "&sum
        Case "SUBTRACT"
                     msgbox "subtraction x-y : "&st
        Case "MULTIPLY"
                     msgbox "multiplication x*y  : "&mult
        Case "DIVIDE"
                     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…”

{ 1 comment… read it below or add one }

parth arora May 29, 2013 at 11:12 am

hey this script has helped me a lot it is easy to understand


Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email