While programming, you will have to make certain decisions and perform different actions based on those decisions.
In such situations, you will be using conditional statements.
In VBScript, there are four types of conditional statements: If…Then, If…..Then…Else, If…Then…..ElseIf, and Select Case.
You will use the VBScript If-Then statement if you want to execute some code when a specific condition is true.
For example, you want to output the message “Welcome” whenever the value of the variable loggedIn is true.
In this case, you will be using If…Then statement in VBS.
If loggedIn = true Then document.write("Welcome") End If
NOTE: If you forget to end the code with End If, you will not get any output.
You will be using VBScript If….Then….Else statement, if you want to select one of two blocks of code to execute.
For example, you want to output the message “Hi, Good Morning” when the value of a variable named “time” is less than or equal to ten and output the message “Hi, Good Day” otherwise.
In such a case, you will be using If….Then…..Else statement.
If timeVBScript If Elseif Statement
You will be using If…..Then…….ElseIf statement, if you have to select one of many blocks of code to execute.
For example, if you want to change the output based on the day of the week, then you have to use If…..Then…….ElseIf statement.
If today="Sunday" Then document.write("Today is Sunday") ElseIf today="Monday" Then document.write("Today is Monday") ElseIf today="Tuesday" Then document.write("Today is Tuesday") ElseIf today="Wednesday" Then document.write("Today is Wednesday") ElseIf today="Thursday" Then document.write("Today is Thursday") ElseIf today="Friday" Then document.write("Today is Friday") ElseIf today="Saturday" Then document.write("Today is Saturday") End IfVBScript SELECT Case Statement
Similar to If…..Then…….ElseIf statement, VBScript Case statement can also be used if you have to select one of many blocks of code to execute.
The same above code can be written like this using Select Case statement.
Select Case today Case "Sunday" document.write("Today is Sunday") Case "Monday" document.write("Today is Monday") Case "Tuesday" document.write("Today is Tuesday") Case "Wednesday" document.write("Today is Wednesday") Case "Thursday" document.write("Today is Thursday") Case "Friday" document.write("Today is Friday") Case "Saturday" document.write("Today is Saturday") End SelectTry the code given below to make the concept clearer.
If Else If Example
Step 1) Copy the code into your editor