Introduction to Conditional Statements: VBScript Tutorial #4
We discussed ‘Constants, Operators and Operators Precedence in the VBScript‘ in my previous 3rd tutorial in ‘Learning VBScript‘ series.
Here, I will be explaining the different Conditional Statements that are used in the VBScript. You need to have a good understanding of these Conditional statements in order to proceed further with the subsequent topics in an easy manner.
This tutorial briefs you about the meaning and types of Conditional Statements in detail along with simple examples for your easy understanding.
Let’s discuss Conditional Statements.
Table of Contents:
Conditional Statements
As the name itself explains, Conditional Statements refer to a set of statements that are written to meet a particular condition. In simple terms, these are such statements that help in the decision-making process by enabling one to check if any specific condition is met or not as per the requirement of a program.
At times a situation may occur when in a piece of code, some actions are required to be performed for taking some decisions and Conditional Statements satisfy this criterion. These statements are widely used during coding, hence they form the basis of a programming language.
Let me quote a simple Example from our day-to-day life to make you understand this more clearly. Let’s consider a scenario that ‘you have to go to some party today but there are some conditions for that. If the cab comes then you will be able to go to the party or else you will have to miss the party.
In such a case, conditional statements can be written as follows:
If a cab comes Then
you will go to the party
Else
you will miss the party
End If
Let’s discuss the various types of Conditional Statements in the VBScript.
Suggested reading =>> Conditional statements in VBA
Various Conditional Statements in the VBScript
VBScript language supports 4 kinds of Conditional Statements.
Most of us must already be aware of these statements while working in other programming languages. Out of these statements, any one statement can be used based on your requirement and it’s up to you to decide and pick the best option for a specific scenario.
Following is the list of Conditional Statements:
- VBScript If…End If Statement
- VBScript If…Else…End If Statement
- VBScript If…ElseIf…Else…End If Statement
- VBScript Select Case…End Select Statement
Let’s discuss each of these statements in detail.
#1) VBScript If…End If Statement
This Conditional Statement is the most basic and widely used one out of all the 4 Conditional Statements available. Also, it is very easy to use and understand.
This is used in those scenarios where there is any specific condition to be met and if that condition is satisfied then some specific lines of statements that are written inside the ‘If…End If’ will be executed.
Let’s understand this with the help of a simple example.
Example:
If you want to display some specific message based on the value of some variable then If…End If Statement can be used in the following manner.
<html> <head> <title>Let’s see implementation of If End If Statement</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val = “Wow” If val = “Wow” Then document.write (“This is the correct statement”) End If </script> </body> </html>
#2) VBScript If…Else…End If Statement
This one is the expansion of the If…End If statement. Under this type, the block of statements is divided into 2 parts – inside the ‘If’ part and inside the ‘Else’ part respectively. Either of the 2 parts is executed immediately depending upon which condition is satisfied.
Let’s understand this with the help of a simple example.
Example:
If there is an expression that will evaluate either True or False. Then in the case of True, some specific message will be displayed and in the case of False, some other message will be displayed.
In such a scenario, you can make use of If…Else…End If Statement in the following way.
<html> <head> <title>Let’s see implementation of If Else Statement</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val1, a, b a=5 b=10 val1 = (a == b) OR (a<=10) If val1 = True Then document.write (“Result of the expression is true”) Else document.write (“Result of the expression is false”) End If </script> </body> </html>
#3) VBScript If…ElseIf…Else…End If Statement
This method is an extension of the earlier one i.e. If…Else…End If Statement. When a set of statements are divided into multiple blocks along with the If and Else part and if some other parts are also required in a code then you can use this Conditional Statement.
At times, a situation may arise when a condition is not satisfied in the ‘If’ part nor in the ‘Else’ part, and some other conditions are required to be checked.
In the above-mentioned cases If…ElseIf…Else…End If Statement can be used.
Let’s understand this clearly with the help of an Example.
Example:
Let’s assume a case where you need to check the data type of a variable as whether it’s of Boolean type, string type, etc. Then in such a scenario you have to check multiple conditions and must move in various blocks one by one and if nothing satisfies the condition then finally, you will move to the Else part of the code.
Let’s take a look at its implementation.
<html> <head> <title>Let’s see implementation of If ElseIf Statement</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val val = “abcd” val1 = VarType(val) If val1 = vbEmpty Then document.write (“This is an Empty variable”) ElseIf val1=vbNull document.write (“This is a Null variable”) ElseIf val1=vbString document.write (“This is a variable of String data type”) ElseIf val1=vbBoolean document.write (“This is a variable of Boolean data type”) Else document.write (“This is not a valid variable so cannot identify its data type”) End If </script> </body> </html>
#4) VBScript Select Case…End Select Statement
This Conditional Statement works in the same manner as the above one i.e. If…ElseIf…Else…End If Statement but as per the observation Select Case is the better option when it comes to working with multiple sets of conditions in a program.
When you work with ‘If…ElseIf…Else…End If Statement’ you have to move each condition or block one by one until you reach the expected one and this consumes a lot of time and effort. Whereas in the case of Select Statement, you can directly move to the exact case without wasting time by going to each condition one by one.
Let’s see the implementation of the Select Case by making reference to the above example itself.
Example:
<html> <head> <title>Let’s see implementation of Select Case Statement</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val val = “abcd” Select Case VarType(val) Case vbEmpty document.write (“This is an Empty variable”) Case vbNull document.write (“This is a Null variable”) Case vbString document.write (“This is a variable of String data type”) Case vbBoolean document.write (“This is a variable of Boolean data type”) Case Else document.write (“This is not a valid variable so cannot identify its data type”) End Select </script> </body> </html>
Conclusion
I hope that by the end of this tutorial of the VBScript Series you will be able to understand the subsequent tutorials of this series with ease and comfort.
Next Tutorial #5: I will brief you about the various types of loops in the VBScript language in my upcoming tutorials.
Stay tuned for much more tutorials on VBScript Series.
Feel free to share your experience working with Conditional Statements and let us know if you have any queries.