In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the usage of If...Then...Else sentences in VBS". In daily operation, I believe many people have doubts about the usage of If...Then...Else sentences in VBS. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the usage of If...Then...Else sentences in VBS". Next, please follow the editor to study!
If...Then...Else statement
Execute a set of statements conditionally based on the value of the expression.
If condition Then statements [Else elsestatements]
Alternatively, use the syntax in block form:
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements]]. . .
[Else
[elsestatements]]
End If
Parameters.
Condition
One or more expressions of the following two types:
A numeric or string expression that evaluates to True or False. If condition is Null, condition is considered False.
An expression that looks like TypeOf objectname Is objecttype. Objectname is a reference to any object, while objecttype is any valid object type. If objectname is an object type specified by objecttype, the expression is True; otherwise False.
Statements
One or more statements executed (separated by colons) if condition is True.
Condition-n
Same as condition.
Elseifstatements
One or more statements executed if the associated condition-n is True.
Elsestatements
One or more statements executed if there is no previous condition or condition-n expression that is True.
Description
For short and simple tests, you can use a single-line form (the first syntax). But the block form (the second syntax) provides stronger structure and adaptability than the single-line form, and is easier to read, maintain, and debug.
Note that in single-line syntax, multiple statements can be executed as a result of If...Then judgment, but all statements must be on the same line and separated by colons, as shown in the following statements:
If A > 10 Then A = A + 1: B = B + A: C = C + B
When the program runs to the If block (the second syntax), condition is tested. If condition is True, the statement after Then is executed. If condition is False, the conditionality (if any) of each ElseIf part is calculated and tested in turn. When a condition that is True is found, the statement after its associated Then is executed. If none of the ElseIf statements is True (or there is no ElseIf clause), the statement after Else is executed. After the statement after Then or Else is executed, the statement after End If continues.
Both the Else and ElseIf clauses are optional. You can place as many ElseIf clauses as you want in an If block, but all must precede the Else clause. If block statements can be nested, that is, contained in another If block statement.
To determine whether a statement is an If block, check what comes after the Then keyword. If there is other non-comment content after the same line of Then, this statement is a single-line If statement.
The If block statement must be the first statement of a line and must end with an End If statement.
To run a single-line statement when the condition is True, use the single-line syntax of the If...Then...Else statement
The following example demonstrates one-line syntax. Please note that the keyword Else is omitted in this example
The copy code is as follows:
Dim myDate 'defines a variable
MyDate = # 2Compact 9pm 'assigns a variable, time: 95-2-13
If myDate < Now Then myDate = Now 'determines the size of the current time, and the smaller one assigns the current time to MyDate.
MsgBox myDate 'outputs the value of the variable myDate. The output is 95-2-13.
To run multiple lines of code, you must use multiline (or block) syntax. Multiline (or block) syntax contains End If statements
The following example demonstrates multiline syntax. As follows:
The copy code is as follows:
Dim myDate 'defines a variable
MyDate = # 2Compact 9pm 'assigns a variable, time: 95-2-13
If myDate < Now Then 'and current time to judge the size
If myDate = Now' is small, the current time is assigned to MyDate and the value of the reassigned myDate is output.
MsgBox myDate 'output: 95-2-13
End If
Run statements separately when the condition is True and False
You can define two executable statement blocks using the If...Then...Else statement: one statement block is run when the condition is True, and another statement block is run when the condition is False. Specific examples are as follows:
The copy code is as follows:
Dim myDate 'defines a variable
MyDate = # 2Compact 222hammer 'assign a variable value, time: 2222-2-13
If myDate < Now Then 'and current time to judge the size
If myDate = Now'is small, the current time is assigned to MyDate and the value of the reassigned myDate is output.
MsgBox myDate
Else
If MsgBox myDate'is large, the value of myDate is output directly. The output is: 2222-2-13.
End If
Judge multiple conditions
A variant of the If...Then...Else statement allows you to choose from multiple conditions, that is, adding an ElseIf clause to extend the functionality of the If...Then...Else statement, allowing you to control based on a variety of possible program flows. Specific examples are as follows:
The copy code is as follows:
Dim value 'defines a variable
The value = 10 'variable is assigned to 10
If value = 0 Then 'determines the value of the variable, and outputs the value of the variable if it is equal
MsgBox value
ElseIf value = 1 Then
MsgBox value
ElseIf value = 2 then
Msgbox value
Else
Msgbox "value is out of range!" If none of them are equal, the output "value is out of range!"
End If
You can add as many ElseIf clauses as you want to provide multiple choices. Using multiple ElseIf clauses can often become cumbersome. A better way to choose among multiple conditions is to use the Select Case statement.
At this point, the study of "the use of If...Then...Else sentences in VBS" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.