VBScript tutorial Lesson 7 use conditional statements

Source: Internet
Author: User

VB tutorial> Lesson 7 use conditional statements
ControlProgramRun

Conditional statements and cyclic statements can be used to control the script process. You can use conditional statements to write VBScript for judgment and repeated operations.Code. 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.

Use select case for judgment

The Select case structure provides a flexible form of the IF... then... elseif structure. You can select multiple statement blocks to execute one of them. The Select case statement provides similar functions as the IF... then... else statement, but can make the code more concise and easy to read.

The Select case structure uses a simple test expression that is only calculated once at the beginning. The result of the expression is compared with the value of each case in the structure. If yes, execute the statement block associated with the case:

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 only calculates one expression at the beginning (only once), while the if... then... the elseif structure calculates the expressions of each elseif statement. These expressions can be different. The Select case structure can be used to replace the IF... then... elseif structure only when the expressions calculated by each elseif statement are the same.

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.