VBScript Conditional Statements
Conditional Statements
Conditional statements are used to perform different actions based on different conditions.
In VBScript, we can use four conditional statements:
- If statement- If you want to execute a series of code when the condition is true, you can use this statement
- If...Then...Else statement- If you want to execute one of two sets of code, you can use this statement
- If...Then...ElseIf statement- If you want to choose one of multiple sets of code to execute, you can use this statement
- Select Case statement- If you want to choose one of multiple sets of code to execute, you can use this statement
If...Then...Else
In the following cases, you can use the If...Then...Else statement:
- Execute a certain piece of code when the condition is true
- Choose one of two pieces of code to execute
If you only execute when the condition is trueonestatement, you can write the code on one line:
In the above code, there is no ..Else.. statement. We simply let the code execute when the condition is true (when i=10)an action。
If you execute when the condition is truemore than onestatement, then you must write one statement per line, and use the keyword "End If" to end the statement:
alert("Hello")
i = i+1
End If
In the above code, there is also no ..Else.. statement. We simply let the code execute when the condition is truemultiple actions。
If you want to execute one statement when the condition is true and another statement when the condition is not true, you must add the keyword "Else":
Example (IE only)
i=hour(time)
If i < 10 Then
document.write("Good morning!")
Else
document.write("Have a nice day!")
End If
</script>
Try it »
In the above code, 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...Then...ElseIf
If you want to choose one of multiple sets of code to execute, you can use the If...Then...ElseIf statement:
Example (IE only)
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 »
Select Case
If you want to choose one of multiple sets of code to execute, you can use the "Select Case" statement:
Example (IE only)
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 »
How the above code works: First, we need a simple expression (often a variable), and this expression is evaluated once. Then, the value of the expression is compared with the value in each Case. If there is a match, the code corresponding to the matched Case is executed.
Other Extensions