Conditional statements

Source: Internet
Author: User
Tags case statement
Control program execution
Conditional statements and cyclic statements can be used to control the Script process. You can use conditional statements to write VBScript code for judgment and repeated operations. The following conditions can be used in VBScript:
If... Then... Else statement
Select Case statement
Use If... Then... Else for judgment
If... Then... Else is used to calculate whether the condition is True or False, and specify the statement to be run based on the calculation result. Generally, the condition is an expression that uses a comparison operator to compare values or variables. For more information about comparison operators, see Comparison operators. If... Then... Else statements can be nested as needed.
Run the statement when the condition is True.
To run a single-row statement when the condition is True, you can use the single-row syntax of the If... Then... Else statement. The following example demonstrates the single line syntax. Note that the keyword Else is omitted in this example.
Sub FixDate ()
Dim myDate
MyDate = #2/13/95 #
If myDate <Now Then myDate = Now
End Sub
To run multi-line code, you must use the multi-line (or block) syntax. The multiline (or block) syntax contains the End If statement, as shown below:
Sub AlertUser (value)
If value = 0 Then
AlertLabel. ForeColor = vbRed
AlertLabel. Font. Bold = True
AlertLabel. Font. Italic = True
End If
End Sub
When the conditions are True and False, run some statements respectively.
You can use the If... Then... Else statement to define two executable statement blocks: when the condition is True, a statement block is run, and when the condition is False, another statement block is run.
Sub AlertUser (value)
If value = 0 Then
AlertLabel. ForeColor = vbRed
AlertLabel. Font. Bold = True
AlertLabel. Font. Italic = True
Else
AlertLabel. Forecolor = vbBlack
AlertLabel. Font. Bold = False
AlertLabel. Font. Italic = False
End If
End Sub
Judge multiple conditions
If... then... an Else statement deformation allows you to select from multiple conditions, that is, add the ElseIf clause to expand If... then... else statements allow you to control a variety of possible program processes. For example:
Sub ReportValue (value)
If value = 0 Then
MsgBox value
ElseIf value = 1 Then
MsgBox value
ElseIf value = 2 then
Msgbox value
Else
Msgbox "value out of range! "
End If
You can add any number of ElseIf clauses to provide multiple options. Using multiple ElseIf clauses is often cumbersome. A better way to Select 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.