• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

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.....Else, If and Select Case.

In this tutorial, you will learn-

VBScript If Then Statement

You will use 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.

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.

VBScript If Else Statement

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 time <= 10 Then
document.write("Hi, Good Morning")
Else
document.write("Hi, Good Day")
End If

VBScript 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 If

VBScript 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 Select

Try the code given below to make the concept clearer.

If Else If: Example

Step 1) Copy the code into your editor

<html>
<head>
<script type="text/vbscript">
Dim age
age = InputBox("Enter your age")
If age<18 Then
document.write("You are too young.")
ElseIf age<45 Then
document.write("You are still young.")
ElseIf age<70 Then
document.write("You are getting older.")
Else
document.write("You are too old.")
End If

</script>

</head>
<body>
</body>
</html>

Step 2) Save the file as condition.html in your preferred location.

Now open the file in Internet Explorer and your screen will look like this with a box asking to enter your age.

VBScript Conditional Statement - Else if, if Else, Select Case

Enter a numeric value, say 22. You will get a message like this.

VBScript Conditional Statement - Else if, if Else, Select Case

Enter different values and observe the output.

Summary

  • Conditional statements are used to make decisions and execute different blocks of code based on the decisions taken.
  • You will use If...Then statement, if you want to execute some code when a specific condition is true.
  • You will use If....Then....Else statement, if you want to select one of two blocks of code to execute.
  • You will use If.....Then.......ElseIf statement and Select Case statement, if you have to select one of many blocks of code to execute.

 

YOU MIGHT LIKE: