VBScript – Flow Control with Conditional Statements

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…”

Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.