VBS Tutorial: VBScript basics-use conditional statements

Source: Internet
Author: User

Use conditional statements to Control Program Execution

You can use conditional statements and cyclic statements 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... ElseStatement used to calculate whether the condition isTrueOrFalseAnd specify the statement to 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... ElseStatements can be nested as needed.

Run the statement when the condition is True.

If the condition isTrueRun a single row statement.If... Then... ElseStatement. The following example demonstrates the single line syntax. Note that the keyword is omitted in this example.Else.

 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. Multiline (or block) syntax includesEnd IfStatement:

 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.

AvailableIf... Then... ElseThe statement defines two executable statement blocks: the condition isTrueWhen a statement block is run, the condition isFalseRun another statement block.

 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... ElseA type of statement deformation allows you to select from multiple conditions, that is, addElseIfClause to expandIf... Then... ElseThe statement function allows you to control program processes based on a variety of possibilities. 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 "The value is out of the range!"     End If

You can add any numberElseIfClause to provide multiple options. Use multipleElseIfClause is often cumbersome. A better way to select multiple conditions is to useSelect CaseStatement.

Use Select Case for judgment

Select CaseStructure providesIf... Then... ElseIfA flexible structure. You can select to execute one of multiple statement blocks.Select CaseFunctions andIf... Then... ElseStatements are similar, but the code can be more concise and easy to read.

Select CaseThe structure uses a simple test expression that is only calculated once at the beginning. The result of the expression isCase. If yesCaseExample code:

 Select Case Document.Form1.CardType.Options(SelectedIndex).Text    Case "MasterCard"        DisplayMCLogo        ValidateMCAccount    Case "Visa"        DisplayVisaLogo        ValidateVisaAccount    Case "American Express"        DisplayAMEXCOLogo        ValidateAMEXCOAccount    Case Else        DisplayUnknownImage        PromptAgainEnd Select

Note:Select CaseThe structure calculates only one expression at the beginning (only once), whileIf... Then... ElseIfStructure CalculationElseIfStatement expressions. These expressions can be different. Only when eachElseIfStatement calculation can only be used if the expressions are the sameSelect CaseStructure substitutionIf... Then... ElseIfStructure.

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.