VBScript Conditional Statement: IF…Else & ElseIF (Example)

โšก Smart Summary

VBScript Conditional Statements let a program make decisions and run different code based on a condition. VBScript provides four forms: If…Then, If…Then…Else, If…Then…ElseIf, and Select Case, which chooses one block from many based on a matched value.

  • ๐Ÿ”€ If…Then: Runs a block of code only when a condition is true, and must end with End If.
  • โ†”๏ธ If…Then…Else: Chooses between two blocks, running one when the condition is true and the other otherwise.
  • ๐Ÿชœ If…Then…ElseIf: Tests several conditions in order and runs the first matching block.
  • ๐ŸŽ›๏ธ Select Case: Compares one variable against many values, a cleaner alternative to long ElseIf chains.
  • โŒจ๏ธ InputBox Demo: The age example reads user input and picks a message based on the value entered.
  • โš ๏ธ End Markers: If blocks need End If and Select Case needs End Select, or the code produces no output.

VBScript Conditional Statement

What is Conditional Statement?

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.

VBScript If Then Statement

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.

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 SELECT Case Statement

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

VBScript SELECT Case Statement

Enter different values and observe the output.

FAQs

Use Select Case when comparing one variable against many fixed values; it is cleaner than a long ElseIf chain. Use ElseIf when the conditions test different variables or ranges.

Yes. A single-line If such as If x=1 Then y=2 needs no End If. End If is only required when the If block spans multiple lines with separate statements.

No. VBScript has no ternary (?:) operator and no built-in IIf function. Use a full If…Then…Else block, or write a small helper function, to select a value based on a condition.

Yes. AI can refactor a repetitive If…ElseIf ladder into an equivalent Select Case block, improving readability. It can also flag conditions that cannot be converted, such as range checks.

AI can spot a missing End If or End Select, wrong comparison operators, and unreachable branches. It explains why a condition never runs and suggests the fix, reducing silent no-output bugs.

Summarize this post with: