In VBScript, the If Else or elseif statement is used to write down decision-making statements. If the specified condition becomes true the code in the if block will be executed otherwise the code in the else block will be executed. We can use different types if statements based on the following scenarios.
- If statement without an Else statement to execute a piece of code only when certain condition is met. The Else block is not required.
- If Else statement to execute either of the blocks based on True and False condition
- Nested If Else Statements to check multiple Boolean expressions.
- We can also use If Else statement with combinations of Logical operator to evaluate Boolean expressions and inside a loop as well.
VBScript If Statement
The VBA If statement is used to execute a piece of code when the desired condition becomes True .The flow diagram of an if statement without else statement in VBScript is as follows.
The following is the syntax of an If Statement in VBScript.
If(Condition) Then
Statement 1
…..
…..
Statement n
End If
Example: Display output in message box only if value of variable a is greater than B
1 2 3 4 5 6 7 8 9 10 |
Dim a, b a = 10 b = 5 If a > b Then MsgBox "A is greater than B" End If |
Output
VBScript If Else Statement
The VBScript if else statement is used to execute either of the block statements based on the evaluation outcome (True or False) of the Boolean expression. The flow diagram of an If and Else statement in VBScript is as follows.
The following is the syntax of an If Else Statement in VBScript.
If(Condition) Then
Statement 1
…..
…..
Statement n
Else
Statement 1
…..
…..
Statement n
End If
Example: Find which number is greater and display the output in the message box.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Function ifElseTest(a, b) If a > b Then MsgBox "A is greater than B" Else MsgBox "B is greater than A" End If End Function Function findGreater() Call ifElseTest(10, 20) 'Passing argument values for ifElseTest function End Function |
Output
If you want to know more about user defined functions. Please refer User-Defined Functions
A bit more Complex If Else Statement Flow
Example: Find which number is greater and check whether the numbers lies between 1 to 9 or it is greater than 9.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Function ifElseTest(a, b) If a > b Then If (a > 1 And a < 10) Then MsgBox "A is greater than B and Value of A lies between 1 to 9" Else MsgBox "A is greater than B and Value of A is greater than 9" End If Else If (b > 1 And b < 10) Then MsgBox "B is greater than A and Value of B lies between 1 to 9" Else MsgBox "B is greater than A and Value of B is greater than 9" End If End If End Function Function findGreaterAndNumbRange() Call ifElseTest(8, 7) End Function |
Output
VBScript Nested If ElseIf Statement
Flow diagram of a nested If and Elseif Statement in VBScript is as follows. The ElseIf statement is used to evaluate a conditional statement only if the previous conditions have not been met.
The following is the syntax of an nested If Elseif – Else statement in VBScript.
If(Condition 1) Then
Statement 1
…..
…..
Statement n
ElseIf (Condition 2) Then
Statement 1
…..
…..
Statement n
ElseIf (Condition 3) Then
Statement 1
…..
…..
Statement n
Else
Statement 1
…..
…..
Statement n
End If
Example: Display day name based on the input value between 1 to 7.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
Function testNestedIfElse(inpNum) If inpNum = 1 Then MsgBox "Sunday" ElseIf inpNum = 2 Then MsgBox "Monday" ElseIf inpNum = 3 Then MsgBox "Tuesday" ElseIf inpNum = 4 Then MsgBox "Wednesday" ElseIf inpNum = 5 Then MsgBox "Thursday" ElseIf inpNum = 6 Then MsgBox "Thursday" ElseIf inpNum = 7 Then MsgBox "Thursday" Else MsgBox "Input is not valid" End If End Function Function callFuction() Call testNestedIfElse(5) End Function |
Output
Conclusion
So in this article we have seen various uses of VBScript conditional if else Statements. I hope you might be able to use them wisely as per the need.