1 / 21

VBScript

VBScript. Session 4. What we learn last session?. Constants VBScript operators. Working with Logical, arithmetic and comparision operators. Subjects for session 4. Conditional statements. If … Then … Else Select Case. Conditional Statements.

linnea
Download Presentation

VBScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VBScript Session 4

  2. What we learn last session? • Constants • VBScript operators. • Working with Logical, arithmetic and comparision operators.

  3. Subjects for session 4 • Conditional statements. • If…Then…Else • Select Case

  4. 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

  5. If Then Else

  6. 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.

  7. 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. Dim myDate myDate = #2/13/95# If myDate < Now Then myDate = Now To run more than one line of code, you must use the multiple-line (or block) syntax. If value = 0 Then AlertLabel.ForeColor = vbRed AlertLabel.Font.Bold = True AlertLabel.Font.Italic = TrueEnd If

  8. 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. 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 EndIf

  9. 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. • If value = 0 • Then Message = “zero” • ElseIf value = 1 then • Message = “one” • ElseIf value = 2 then • Message = “two” • Else • Message = "Value out of range!" • EndIf

  10. 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 severalalternatives is the Select Case statement.

  11. Select Case

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

  13. Making Decisions with Select Case • A SelectCase 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

  14. Making Decisions with Select Case • SelectCase strCreditCard Case "MasterCard" DisplayMCLogo ValidateMCAccount Case "Visa" DisplayVisaLogo ValidateVisaAccount Case "American Express" DisplayAMEXCOLogo ValidateAMEXCOAccount CaseElse DisplayUnknownImage PromptAgain EndSelect

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

  16. Making Decisions with Select Case • The CaseElse 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 CaseElse statement in your SelectCase block to handle unforeseen testexpression values. • SelectCase statements can be nested. Each nested SelectCase statement must have a matching EndSelect statement.

  17. Lab 4.1

  18. 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 If…Then…Else Statement

  19. 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.

  20. 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..then…End If statement once and Select Case once.

  21. Make sure to visit us • Tutorials • Articles • Projects • And much more www.AdvancedQTP.com

More Related