VBS Tutorial: VBScript-Select Case statement

Source: Internet
Author: User

Select Case statement

Execute several sets of statements based on the expression value.

Select Case testexpression
[Case expressionlist-n
[statements-n]] . . .
[Case Else expressionlist-n
[elsestatements-n]]
End Select

Parameters

Testexpression

Any number or string expression.

Expressionlist-n

For exampleCaseThis parameter is required when it appears. List of the boundaries of one or more expressions.

Statements-n

One or more statements executed when testexpression matches any part of expressionlist-n.

Elsestatements-n

WhenTestexpressionAndCaseIf any part of the clause does not match, one or more statements are executed.

Description

IfTestexpressionWith anyCase ExpressionlistIf the expression matches, run the following command:CaseClause and nextCaseStatement Between clauses. For the last clause, the clause is executedEnd SelectAnd then the control is forwardedEnd Select. For exampleTestexpressionWith multipleCaseClauseExpressionlistIf the expression matches, only the statement after the first match is executed.

Case Else indicates that elsestatements is executed if no match is found between testexpression and expressionlist of any other Case options. Although not necessary, it is best to place the Case Else statement in the Select Case block to handle unforeseen testexpression values. If no Case expressionlist matches testexpression and no Case Else statement expressionlist, the statement after End Select is executed.

Select CaseThe statement can be nested, with each layer nestedSelect CaseThe statement must matchEnd SelectStatement.

The following example illustrates how to useSelect CaseStatement:

Dim Color, MyVarSub ChangeBackground (Color)   MyVar = lcase (Color)      Select Case MyVar           Case "red"    document.bgColor = "red"           Case "green"  document.bgColor = "green"           Case "blue"   document.bgColor = "blue"           Case Else     MsgBox "Select another color"      End SelectEnd Sub

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.