VBS TUTORIAL: VBScript statement-if ... Then ... Else Statement _vbs

Source: Internet
Author: User
Tags case statement current time

If ... Then ... Else statement

Conditionally executes a set of statements based on the value of an expression.

If condition Then statements [Else elsestatements]

Or, use block-form syntax:

If condition Then
[Statements]
[ElseIf condition-n Then
[Elseifstatements]] . . .
[Else
[Elsestatements]]
End If

Parameters
Condition

One or more of the following two types of expressions:

A numeric or string expression that evaluates to True or False. If condition is Null, the condition is treated as False.

An expression of the form TypeOf objectname is objecttype. ObjectName is a reference to any object, and objecttype is any valid object type. If objectname is an object type specified by objecttype, the expression is True; otherwise, False.

Statements

If condition is True, one or more (separated by colons) statements are executed.

Condition-n

With condition.

Elseifstatements

One or more statements that are executed if the associated condition-n is true.

Elsestatements

One or more statements that are executed if the previous condition or condition-n expression is True.

Description
For short, simple tests, you can use Single-line form (the first syntax). But block form (the second syntax) provides a stronger structure and adaptability than single-line form, and is easier to read, maintain, and debug.

Note In Single-line syntax, you can execute multiple statements as If ... Then The result of the judgment, but all statements must be on the same line and separated by a colon, as shown in the following statement:

If a > Then a = a + 1:b = b + a:c = C + b
When the program runs to the If block (the second syntax), the condition is tested. If condition is True, the statement after Then is executed. If condition is False, the conditional (if any) of each ElseIf part is computed and tested in turn. When a condition that is True is found, its associated Then statement is executed. If no ElseIf statement is True (or there is no ELSEIF clause), then the statement after Else is executed. After executing the statement after Then or ELSE, the statement after end If is executed.

Both Else and ELSEIF clauses are optional. Any number of ElseIf clauses can be placed in an If block, but must precede the ELSE clause. If block statements can be nested, they are included in another if block statement.

To determine if a statement is an if block, check what is after the Then keyword. If there are other non-annotated content after the same row of Then, this statement is a single-line form of if statement.

An IF block statement must be the first statement of a row, and must end with an ending IF statement.


To run a single-line statement when the condition is True, use the If ... Then ... Single-line syntax of Else statement

The following example demonstrates the Single-line syntax. Please note that this example omits the keyword Else

Copy Code code as follows:

Dim mydate ' defines a variable
mydate = #2/13/95# ' Assign value to variable, time: 95-2-13
If MyDate < now Then mydate = Now is the size of the current time and the current time is assigned to a small amount mydate
MsgBox mydate ' output variable mydate value, output as: 95-2-13


To run multiple lines of code, you must use multiple lines (or blocks) of syntax. MultiRow (or block) syntax contains an end IF statement

The following example demonstrates multiple lines of syntax. As shown below:

Copy Code code as follows:

Dim mydate ' defines a variable
mydate = #2/13/95# ' Assign value to variable, time: 95-2-13
If MyDate < Now Then ' judge size with current time
MyDate = Now ' small ' assigns the current time to the mydate, and outputs the value of the mydate that is being assigned.
MsgBox mydate ' output is: 95-2-13
End If


Run the statement separately if the condition is True and False

You can use the If ... Then ... Else statement defines two executable statement blocks: A block of statements runs when the condition is True, and another block of statements runs when False. The details are as follows:

Copy Code code as follows:

Dim mydate ' defines a variable
mydate = #2/13/2222# ' Assign value to variable, time: 2222-2-13
If MyDate < Now Then ' judge size with current time
MyDate = Now ' small ' assigns the current time to the mydate, and outputs the value of the mydate that is being assigned.
MsgBox mydate
Else
MsgBox mydate ' Large then direct output mydate value, output as: 2222-2-13
End If


To judge a number of conditions

If ... Then ... A variant of the ELSE statement allows you to select from multiple conditions, that is, to add a ElseIf clause to augment the If ... Then ... Else statement, which allows you to control the process based on a variety of possible programs. The details are as follows:

Copy Code code as follows:

Dim value ' defines a variable
Value = 10 ' variable assignment is 10
If value = 0 Then ' to determine the value of the variable, if equal, output the value of the variable
MsgBox value
ElseIf value = 1 Then
MsgBox value
ElseIf value = 2 Then
Msgbox value
Else
Msgbox "value out of range!" "' If all is not equal, the output value is out of range!" ”
End If


You can add any number of ElseIf clauses 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.