VB conditions



Conditional statements

Conditional statements are used to perform different operations according to different situations.

In VBScript, we can use four conditional statements:

  • If stat statement ement - If you want to execute when the condition is true For a series of codes, you can use this statement

  • ##If...Then...Else statement - If you want to execute one of the two sets of codes, you can Use this statement

  • If...Then...ElseIf statement - If you want to select one of multiple sets of codes to execute, you can use this statement

  • Select Case Statement - If you want to select one of multiple sets of codes to execute, you can use this statement


If...Then...Else

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

  • In the condition When it is true, execute a certain piece of code

  • Select one of the two pieces of code to execute

If only execute when the condition is true

One statement, you can write the code as one line:

If i=10 Then alert("Hello")
In the above code, there is no ..Else.. statement. We just have the code perform

one operation when the condition is true (when i=10).

If

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

If i=10 Then
alert("Hello")
i = i+1
End If
In the above code, there is also no ..Else.. statement. We just have the code perform

multiple operations when the condition is true.

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

Example

实例(仅适用于 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>

Run instance»Click the "Run instance" button to view the online instance

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


If...Then...ElseIf

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

Instance

实例(仅适用于 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>

Run Instance»Click the "Run Instance" button to view the online instance



Select Case

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

Example

实例(仅适用于 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>

Run Example»

Click the "Run Example" button to view the online example

How the above code works: First, we need a simple expression (often is a variable), and the expression will be evaluated once. The value of the expression is then compared to the value in each Case. If there is a match, the code corresponding to the matched Case will be executed.