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...ElseIn 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
One statement, you can write the code as one line:
one operation when the condition is true (when i=10).
Ifmore 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:
i = i+1
End If
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":实例(仅适用于 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
If...Then...ElseIfIf you want to select one of multiple sets of codes to execute, you can use the If...Then...ElseIf statement:
实例(仅适用于 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 CaseIf you want to select one of multiple sets of codes to execute, you can use the "Select Case" statement:
实例(仅适用于 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>
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.