Latest web development tutorials

VBScript conditional statement

Conditional statements

Conditional statement used to perform different actions depending on the situation.

In VBScript, we can use four conditional statements:

  • If stat statement ement - if you want to perform a series of code if the condition is true, you can use this statement
  • If ... Then ... Else statement - if you want to do one of two sets of code, you can use this statement
  • If ... Then ... ElseIf statement - if you want to select one of many sets of lines to execute, you can use this statement
  • Select Case statement - if you want to select one of many sets of lines to execute, you can use this statement

If ... Then ... Else

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

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

If onlyone statement when the condition is true, you can write the code for the line:

If i=10 Then alert("Hello")

In the above code, there is no ..Else .. statement. We just let the code if the condition is true (when i = 10 time) to performan operation.

If you executemore than one statement when the condition is true, then we must write a statement in a row, then use the keyword "End If" to end this statement:

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

In the above code, the same does not ..Else .. statement. We just let the code to perform anumber of actionsif the condition is true.

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

Examples (only for IE)

<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"

In the above code, when the condition is true it will execute the first code, the second paragraph of the code executed when the condition is not satisfied (when i is greater than 10).


If ... Then ... ElseIf

If you want to select one of many sets of lines to execute, you can use the If ... Then ... ElseIf statement:

Examples (only for IE)

<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"


Select Case

If you want to select one of many sets of lines to execute, you can use the "Select Case" statement:

Examples (only for IE)

<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"

The above code works: First, we need a simple expression (usually a variable), and this expression is evaluated once operations. Then, as the value of the expression will be the value of each Case comparison. If they match, matched Case corresponding code will be executed.