Working with conditional statements

Source: Internet
Author: User
Tags add case statement comparison end expression
Condition | Statement Control program execution
Use conditional statements and circular statements to control the Script's process. Use conditional statements to write VBScript code for judging and repeating operations. The following conditional statements can be used in VBScript:

If ... Then ... Else statement
Select Case Statement
Use If ... Then ... Else to judge
If ... Then ... The Else statement evaluates to whether the condition is True or False and specifies the statement to run based on the result of the calculation. Typically, a condition is an expression that compares a value or a variable using a comparison operator. For more information about comparison operators, see comparison operators. If ... Then ... The Else statement can be nested as needed.

Run statement when condition is True
To run a single-line statement when the condition is True, use the If ... Then ... The single line syntax of the Else statement. The following example demonstrates the Single-line syntax. Please note that this example omits the keyword Else.

Sub Fixdate ()
Dim mydate
MyDate = #2/13/95#
If MyDate < now Then mydate = Now
End Sub

To run multiple lines of code, you must use multiple lines (or blocks) of syntax. The MultiRow (or block) syntax contains the end If statement, as follows:

Sub Alertuser (value)
If value = 0 Then
Alertlabel.forecolor = vbred
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
End If
End Sub

Run certain statements when 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.

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

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. 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 choices. Using multiple ELSEIF clauses can often become cumbersome. A better way to choose among multiple conditions is to use the Select Case statement.

Use Select case to judge
The Select case structure provides an If ... Then ... An alternative form of a ELSEIF structure that you can select from multiple statement blocks to execute one of them. The Select Case statement provides functionality with the If ... Then ... Else statements are similar, but can make the code more concise and readable.

The Select case structure begins with a simple test expression that is evaluated only once. The result of an expression is compared to the value of each case in the structure. If it matches, the statement block associated with the case is executed:

Select case Document.Form1.CardType.Options (selectedindex). Text
Case "MasterCard"
Displaymclogo
Validatemcaccount
Case "Visa"
Displayvisalogo
Validatevisaaccount
Case "American Express"
Displayamexcologo
Validateamexcoaccount
Case Else
Displayunknownimage
Promptagain
End Select

Note that the Select case structure evaluates only one expression at the beginning (once only), and If ... Then ... The ELSEIF structure computes an expression for each ELSEIF statement, which can vary. You can use the Select case structure instead of if ... when each ELSEIF statement evaluates to an expression that is the same. Then ... ELSEIF structure.



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.