Vous êtes sur la page 1sur 21

VBScript

Session 4

Dani Vainstein

What we learn last session?

Constants VBScript operators. Working with Logical, arithmetic and comparision operators.

Dani Vainstein

Subjects for session 4

Conditional statements.
IfThenElse Select Case

Dani Vainstein

Conditional Statements
You can control the flow of your script with conditional statements and looping statements. Using conditional statements, you can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:
If...Then...Else statement Select Case statement

Dani Vainstein

If Then Else

Dani Vainstein

Making Decisions Using If...Then...Else


The If...Then...Else statement is used to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run. Usually the condition is an expression that uses a comparison operator to compare one value or variable with another. If...Then...Else statements can be nested to as many levels as you need.

Dani Vainstein

Making Decisions Using If...Then...Else


To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement. The following example shows the single-line syntax. Notice that this example omits the Else keyword.
DimmyDate myDate Dim myDate= =#2/13/95# #2/13/95# myDate IfmyDate myDate< <Now NowThen ThenmyDate myDate= =Now Now If

To run more than one line of code, you must use the multiple-line (or block) syntax.
Ifvalue value= =0 0Then Then If AlertLabel.ForeColor= =vbRed vbRed AlertLabel.ForeColor AlertLabel.Font.Bold= =True True AlertLabel.Font.Bold AlertLabel.Font.Italic= =True True AlertLabel.Font.Italic EndIf If End
Dani Vainstein 7

Making Decisions Using If...Then...Else


Running certain statements if a condition is true and Running others if a condition is false
You can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False. Ifvalue value= =0 0Then Then If AlertLabel.ForeColor= =vbRed vbRed AlertLabel.ForeColor AlertLabel.Font.Bold= =True True AlertLabel.Font.Bold AlertLabel.Font.Italic= =True True AlertLabel.Font.Italic Else Else AlertLabel.Forecolor= =vbBlack vbBlack AlertLabel.Forecolor AlertLabel.Font.Bold= =False False AlertLabel.Font.Bold AlertLabel.Font.Italic= =False False AlertLabel.Font.Italic EndIf If End
Dani Vainstein 8

Making Decisions Using If...Then...Else


Deciding Between Several Alternatives
A variation on the If...Then...Else statement allows you to choose from several alternatives. Adding ElseIf clauses expands the functionality of the If...Then...Else statement so you can control program flow based on different possibilities.
Ifvalue value= =0 0 If ThenMessage Message= =zero zero Then ElseIfvalue value= =1 1then then ElseIf Message= =one one Message ElseIfvalue value= =2 2then then ElseIf Message= =two two Message Else Else Message= ="Value "Valueout outof ofrange!" range!" Message EndIf If End
Dani Vainstein 9

Making Decisions Using If...Then...Else


You can add as many ElseIf clauses as you need to provide alternative choices. Extensive use of the ElseIf clauses often becomes cumbersome. A better way to choose between several alternatives is the Select Case statement.

Dani Vainstein

10

Select Case

Dani Vainstein

11

Making Decisions with Select Case


Executes one of several groups of statements, depending on the value of an expression. The Select Case structure provides an alternative to If...Then...ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more efficient and readable.

Dani Vainstein

12

Making Decisions with Select Case


A Select Case structure works with a single test expression that is evaluated once, at the top of the structure. The result of the expression is then compared with the values for each Case in the structure. If there is a match, the block of statements associated with that Case is executed

Dani Vainstein

13

Making Decisions with Select Case


SelectCase CasestrCreditCard strCreditCard Select Case"MasterCard" "MasterCard" Case DisplayMCLogo DisplayMCLogo ValidateMCAccount ValidateMCAccount Case"Visa" "Visa" Case DisplayVisaLogo DisplayVisaLogo ValidateVisaAccount ValidateVisaAccount Case"American "AmericanExpress" Express" Case DisplayAMEXCOLogo DisplayAMEXCOLogo ValidateAMEXCOAccount ValidateAMEXCOAccount CaseElse Else Case DisplayUnknownImage DisplayUnknownImage PromptAgain PromptAgain EndSelect Select End
Dani Vainstein 14

Making Decisions with Select Case


If testexpression matches any Case expressionlist expression, the statements following that Case clause are executed up to the next Case clause, or for the last clause, up to End Select. Control then passes to the statement following End Select. If testexpression matches an expressionlist expression in more than one Case clause, only the statements following the first match are executed.

Dani Vainstein

15

Making Decisions with Select Case


The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist in any of the other Case selections. Although not required, it is a good idea to have a Case Else statement in your Select Case block to handle unforeseen testexpression values. Select Case statements can be nested. Each nested Select Case statement must have a matching End Select statement.

Dani Vainstein

16

Lab 4.1

Dani Vainstein

17

Lab 4.1
Write a small program:
Declare a constant PASSWORD Declare two variables strPsw and Name. If the password is OK, Wellcome the user in the reporter (micPass) Else , display a message in the Reporter (micFail) Use the IfThenElse Statement

Dani Vainstein

18

Lab 4.2
Write a small program:
Declare a variable iNumber Display in the reporter the name of the number (one, two) If the number is not between 0 and 10 display a message out of range Use the select Case statement.

Dani Vainstein

19

Lab 4.3
Write a small program:
Declare a variable iNumber, bSign Like in 4.2, but a number can be from -10 to 10 Display in words, the value of the number i.e. for -2 (Minus two), and for 4 (Plus Four) Otherwise display a message out of range Use if..thenEnd If statement once and Select Case once.

Dani Vainstein

20

Make sure to visit us


Tutorials Articles Projects And much more www.AdvancedQTP.com

21