VBScript
VBScript
Session 4
What we learn last session?
What we learn last session?
Constants Constants
VBScript operators. VBScript operators.
Working with Logical, arithmetic and Working with Logical, arithmetic and comparision operators.
Subjects for session 4
Subjects for session 4
Conditional statements.
Conditional statements.
If
If
……Then
Then
……Else
Else
Select Case
Conditional Statements
Conditional Statements
You can control the flow of your script with conditional You can control the flow of your script with conditional statements and looping statements.
statements and looping statements.
Using conditional statements, you can write VBScript code Using conditional statements, you can write VBScript code that makes decisions and repeats actions.
that makes decisions and repeats actions.
The following conditional statements are available in The following conditional statements are available in VBScript:
VBScript:
If
If ......ThenThen......ElseElse statementstatement
Select Case
If Then Else
Making Decisions Using
Making Decisions Using
If...Then...Else
If...Then...Else
The
The If...Then...ElseIf...Then...Else statement is used tostatement is used to
evaluate whether a condition is
evaluate whether a condition is TrueTrue oror
False
False and, depending on the result, toand, depending on the result, to
specify one or more statements to run. specify one or more statements to run. Usually the condition is an expression Usually the condition is an expression that uses a comparison operator to that uses a comparison operator to compare one value or variable with compare one value or variable with another.
another.
If...Then...Else
If...Then...Else statements can bestatements can be
nested to as many levels as you need. nested to as many levels as you need.
Making Decisions Using
Making Decisions Using
If...Then...Else
If...Then...Else
To run only one statement when a condition is
To run only one statement when a condition is TrueTrue, use the, use the
single-line syntax for the
single-line syntax for the If If ...ThenThen...ElseElse statement.statement.
The following example shows the single-line syntax. The following example shows the single-line syntax. Notice that this example omits the
Notice that this example omits the ElseElse keyword.keyword.
Dim
Dim myDatemyDate
myDate = #2/13/95# myDate = #2/13/95#
If
If myDate < NowmyDate < NowThenThen myDate =myDate =NowNow
To run more than one line of
To run more than one line of code, you must use thecode, you must use the multiple-line (or block) syntax.
multiple-line (or block) syntax. If
If value = 0value = 0ThenThen
AlertLabel.
AlertLabel.ForeColor = ForeColor = vbRedvbRed AlertLabel.Font.Bold =
AlertLabel.Font.Bold =TrueTrue
AlertLabel.Font.Italic =
AlertLabel.Font.Italic =TrueTrue
End If
Making Decisions Using
Making Decisions Using
If...Then...Else
If...Then...Else
Running certain statements if a condition is true and Running Running certain statements if a condition is true and Running others if a condition is false
others if a condition is false
You can use an
You can use an If If ...ThenThen...ElseElse statement to define two blocks of statement to define two blocks of
executable statements: executable statements:
one block to run if the condition is
one block to run if the condition is TrueTrue, the other block to run if the, the other block to run if the
condition is
condition is FalseFalse..
If
If value = 0value = 0 ThenThen
AlertLabel.ForeColor = vbRed AlertLabel.ForeColor = vbRed AlertLabel.Font.Bold =
AlertLabel.Font.Bold = TrueTrue
AlertLabel.Font.Italic =
AlertLabel.Font.Italic = TrueTrue
Else
Else
AlertLabel.Forecolor = vbBlack AlertLabel.Forecolor = vbBlack AlertLabel.Font.Bold =
AlertLabel.Font.Bold = FalseFalse
AlertLabel.Font.Italic =
AlertLabel.Font.Italic = FalseFalse
End
Making Decisions Using
Making Decisions Using
If...Then...Else
If...Then...Else
Deciding Between Several Alternatives Deciding Between Several Alternatives
A variation on the
A variation on the If If ...ThenThen...Else...Else statement allows you tostatement allows you to
choose from several alternatives. choose from several alternatives. Adding
Adding ElseIf ElseIf clauses expands the functionality of clauses expands the functionality of thethe
If
If ...ThenThen...ElseElse statement so you can control program flowstatement so you can control program flow
based on different possibilities. based on different possibilities.
If
If value = 0value = 0
Then
Then Message =Message = ““zerozero””
ElseIf
ElseIf value = 1value = 1 thenthen
Message =
Message = ““oneone””
ElseIf
ElseIf value = 2value = 2 thenthen
Message =
Message = ““twotwo””
Else
Else
Message = "Value out of range!" Message = "Value out of range!"
End
Making Decisions Using
Making Decisions Using
If...Then...Else
If...Then...Else
You can add as many
You can add as many ElseIf ElseIf clauses as you need to provideclauses as you need to provide
alternative choices. alternative choices. Extensive use of the
Extensive use of the ElseIf ElseIf clauses often becomesclauses often becomes
cumbersome. cumbersome.
A better way to choose between several A better way to choose between several alternatives is the
Select Case
Making Decisions with Select
Making Decisions with Select
Case
Case
Executes one of several groups of statements, depending on Executes one of several groups of statements, depending on the value of an expression.
the value of an expression. The
The SelectSelect CaseCase structure provides an alternative tostructure provides an alternative to
If
If ...ThenThen...ElseIf ElseIf for selectively executing one block of for selectively executing one block of
statements from among multiple blocks of statements. statements from among multiple blocks of statements. A
A SelectSelect CaseCase statement provides capability similar to thestatement provides capability similar to the
If
If ...ThenThen...ElseElse statement, but it makes code morestatement, but it makes code more
efficient and readable. efficient and readable.
Making Decisions with Select
Making Decisions with Select
Case
Case
AA SelectSelect CaseCase structure works with a single test expressionstructure works with a single test expression
that is evaluated once, at the top of the structure. that is evaluated once, at the top of the structure.
The result of the expression is then compared with the The result of the expression is then compared with the values for each
values for each CaseCase in the structure.in the structure.
If there is a match, the block of statements associated with If there is a match, the block of statements associated with that
Making Decisions with Select
Making Decisions with Select
Case
Case
Select
Select CaseCase strCreditCardstrCreditCard
Case
Case "MasterCard""MasterCard"
DisplayMCLogo DisplayMCLogo ValidateMCAccount ValidateMCAccount
Case
Case "Visa""Visa"
DisplayVisaLogo DisplayVisaLogo ValidateVisaAccount ValidateVisaAccount
Case
Case "American Express""American Express"
DisplayAMEXCOLogo DisplayAMEXCOLogo ValidateAMEXCOAccount ValidateAMEXCOAccount
Case
Case ElseElse
DisplayUnknownImage DisplayUnknownImage PromptAgain
PromptAgain
End
Making Decisions with Select
Making Decisions with Select
Case
Case
IfIf testexpressiontestexpression matches anymatches any CaseCase expressionlist expressionlist
expression, the statements following that
expression, the statements following that CaseCase clause areclause are
executed up to the next
executed up to the next CaseCase clause, or for the last clause,clause, or for the last clause,
up to
up to EndEnd SelectSelect..
Control then passes to the statement following
Control then passes to the statement following EndEnd SelectSelect..
If
If testexpressiontestexpression matches anmatches an expressionlist expressionlist expression inexpression in more than one
more than one CaseCase clause, only the statements followingclause, only the statements following
the first match are executed. the first match are executed.
Making Decisions with Select
Making Decisions with Select
Case
Case
TheThe CaseCase ElseElse clause is used to indicate theclause is used to indicate the elsestatementselsestatements
to be executed if no match is found between the to be executed if no match is found between the testexpression
testexpression and anand an expressionlist expressionlist in any of the otherin any of the other
Case
Case selections.selections.
Although not required, it is a good idea to have a
Although not required, it is a good idea to have a CaseCase ElseElse
statement in your
statement in your SelectSelect CaseCase block to handle unforeseenblock to handle unforeseen
testexpression
testexpression values.values.
Select
Select CaseCase statements can be nested. Each nestedstatements can be nested. Each nested SelectSelect
Case
Case statement must have a matchingstatement must have a matching EndEnd SelectSelect
statement. statement.
Lab 4.1
Lab 4.1
Lab 4.1
Write a small program:
Write a small program:
Declare a constant PASSWORD Declare a constant PASSWORD
Declare two variables strPsw and Name. Declare two variables strPsw and Name.
If the password is OK, Wellcome the user in the reporter If the password is OK, Wellcome the user in the reporter (micPass)
(micPass)
Else , display a message in the Reporter (micFail) Else , display a message in the Reporter (micFail) Use the If
Lab 4.2
Lab 4.2
Write a small program:
Write a small program:
Declare a variable iNumber Declare a variable iNumber
Display in the reporter the name of the number (
Display in the reporter the name of the number (““oneone””,,
““twotwo””))
If the number is not between 0 and 10 display a If the number is not between 0 and 10 display a message
message ““out of rangeout of range””
Use the select Case statement. Use the select Case statement.
Lab 4.3
Lab 4.3
Write a small program:
Write a small program:
Declare a variable iNumber, bSign Declare a variable iNumber, bSign
Like in 4.2, but a number can be from -10 to 10 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 Display in words, the value of the number i.e. for -2 (Minus two), and for 4 (Plus Four)
(Minus two), and for 4 (Plus Four)
Otherwise display a message out of range Otherwise display a message out of range Use if..then
Use if..then……End If statement once and Select CaseEnd If statement once and Select Case once.