Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VBScript conditional statement


May 13, 2021 VBScript


Table of contents


VBScript conditional statement

VBScript conditional statements allow you to control the flow of scripts and write VBScript code for judgment and repetition.

Conditional statement

Conditional statements are used to perform different operations depending on the situation.

In VBScript, we can use four conditional statements:

  • If stat statement - If you want to execute a series of code when the condition is true, you can use it
  • If... T hen... Else statement - If you want to execute one of two sets of code, you can use it
  • If... T hen... ElseIf statement - If you want to select one of several sets of code to execute, you can use it
  • Select Case statement - If you want to select one of several sets of code to execute, you can use it

If... T hen... Else

In the following cases, you can use If... T hen... Else statement:

  • When the condition is true, a piece of code is executed
  • Select one of the two pieces of code to execute

If you execute only one statement when the condition is true, you can write the code as one line:

If i=10 Then alert("Hello")

In the code above, there is no .. E lse.. S tatement. W e only have the code perform an action when the condition is true (when i is 10).

If more than one statement is executed when the condition is true, you must write a statement on one line and then end the statement with the keyword "End If":

If i=10 Then
alert("Hello")
i = i+1
End If

In the code above, there is also no .. E lse.. S tatement. We only let the code do more than one operation when the condition is true.

If you want to execute a statement when the condition is true and another statement when the condition is not true, you must add the keyword "Else":

Instances (IE only)

<script type="text/vbscript">
i=hour(time)
If i < 10 Then
document.write("Good morning!")
Else
document.write("Have a nice day!")
End If
</script>

Try it out . . .

In the code above, the first piece of code is executed when the condition is true, and the second piece of code is executed when the condition is not true (when i is greater than 10).


If... T hen... ElseIf

If you want to select one of several sets of code to execute, you can use If... T hen... ElseIf statement:

Instances (IE only)

<script type="text/vbscript">
i=hour(time)
If i = 10 Then
document.write("Just started...!")
ElseIf i = 11 Then
document.write("Hungry!")
ElseIf i = 12 Then
document.write("Ah, lunch-time!")
ElseIf i = 16 Then
document.write("Time to go home!")
Else
document.write("Unknown")
End If
</script>

Try it out . . .


Select Case

If you want to select one of several sets of code to execute, you can use the Select Case statement:

Instances (IE only)

<script type="text/vbscript">
d=weekday(date)
Select Case d
Case 1
document.write("Sleepy Sunday")
Case 2
document.write("Monday again!")
Case 3
document.write("Just Tuesday!")
Case 4
document.write("Wednesday!")
Case 5
document.write("Thursday...")
Case 6
document.write("Finally Friday!")
Case else
document.write("Super Saturday!!!!")
End Select
</script>

Try it out . . .

How the above code works: First, we need a simple expression (often a variable), and this expression is evaluated once. T he value of the expression is then compared to the value in each Case. If matched, the code for the matched Case is executed.

In this section, you've learned about VBScript conditional statements, and in the next section, you'll touch VBScript loop statements, which are often used together!