Using If...Then...Else
Syntax
If condition Then statements [Else elsestatements ]
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements] ...
[Else
[elsestatements]]
End If
Ex:
If condition = True Then
... the code that executes if the condition is satisfied
End If
Ex:
If bShowDetail Then MsgBox "More details. . . "
example:
sMsg = "Either you're not born yet or you're getting too old for this stuff!"
If nAge <= 0 Or nAge > 120 Then
MsgBox sMsg
bFail = True
End If
Ex:
If MyFavoriteRestaurantOpen = True Then
Msgbox "Go To My Favorite Restaurant!"
End If
If MyFavoriteRestaurantOpen = False Then
Msgbox "Go Home and Cook!"
End If
Ex:
If MyFavoriteRestaurantOpen = True Then
Msgbox "Go To My Favorite Restaurant!"
Else
Msgbox "Go Home and Cook!"
End If
Ex:
If nAge = 0 Then
MsgBox "Welcome to the human race!"
ElseIf nAge < 0 Then
MsgBox "You have to grow up a bit before you start using VBScript!"
ElseIf nAge > 0 And Age < 10 Then
MsgBox "If you're bold enough, you must be old enough."
ElseIf nAge > 120 Then
MsgBox "You're getting too old for this stuff!"
Else
MsgBox "You're at the perfect age to get started!"
End If
Allows for conditional execution of a block of code, typically out of three or
more code blocks, based on some condition. Use the Select Case statement as
an alternative to complex nested If...Then...Else statements.
Select Case
Syntax
Select Case testexpression
[Case expressionlist-n
[statements-n]] ...
[Case Else
[elsestatements]]
End Select
A Select Case statement provides capability similar to the If...Then...Else
statement, but it makes code more efficient and readable. Select Case
structure is defined as follows:
Select Case expression
Case exp-1
...this is the code that executes if exp-1 matches expression
Case exp-2, exp-3
...this is the code that executes if exp-2 or exp-3 matches expression
Case exp-4
...this is the code that executes if exp-4 matches expression
.
.
.
Case Else
...this is the code that executes if none matches expression
End Select
Example
The following example uses Select Case to read a variable populated by the
user and determine the name of the user's operating system:
Select Case Left(Environment.Value("OSVersion"), 1)
Case 1 : varOSDesc = "Windows NT"
Case 2 : varOSDesc = "Windows 98"
Case 3 : varOSDesc = "Windows 95"
Case 4 : varOSDesc = "Windows 3.11"
Case 5 : varOSDesc = "Windows 2000"
Case 6 : varOSDesc = "Windows ME"
Case 7 : varOSDesc = "Windows XP"
Case Else : varOSDesc = "OS is unknown"
End Select
Using Control Structures to Make Code Repeat:
Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is False; others repeat statements until a condition is True. There are also loops that repeat statements a specific number of times.
The following looping statements are available in VBScript:
Do...Loop: Loops while or until a condition is True.
While...Wend: Loops while a condition is True.
For...Next: Uses a counter to run statements a specified number of times.
For Each...Next: Repeats a group of statements for each item in a collection or
each element of an array.
Using For...Next Statement:
Defines a loop that executes a given number of times, as determined by a loop counter. To use the For...Next loop, you must assign a numeric value to a counter variable. This counter is either incremented or decremented automatically with each iteration of the loop. In the For statement, you specify the value that is to be assigned to the counter initially and the maximum value the counter will reach for the block of code to be executed. The Next statement marks the end of the block of code that is to execute repeatedly, and also serves as a kind of flag that indicates the counter variable is to be modified.
Syntax
For counter = start To end [Step stepcounter]
[statements]
[Exit For]
[statements]
Next
Arguments
Argument Description
counter
Numeric variable used as a loop counter. The variable can't be an array
element or an element of a user-defined type.
start Initial value of counter.
end Final value of counter.
step
Amount counter is changed each time through the loop. If not specified, step defaults to one.
statements
One or more statements between For and Next that are executed the specified number of times.
Using If...Then...Else
Syntax
If condition Then statements [Else elsestatements ]
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements] ...
[Else
[elsestatements]]
End If
Ex:
If condition = True Then
... the code that executes if the condition is satisfied
End If
Ex:
If bShowDetail Then MsgBox "More details. . . "
example:
sMsg = "Either you're not born yet or you're getting too old for this stuff!"
If nAge <= 0 Or nAge > 120 Then
MsgBox sMsg
bFail = True
End If
Ex:
If MyFavoriteRestaurantOpen = True Then
Msgbox "Go To My Favorite Restaurant!"
End If
If MyFavoriteRestaurantOpen = False Then
Msgbox "Go Home and Cook!"
End If
Ex:
If MyFavoriteRestaurantOpen = True Then
Msgbox "Go To My Favorite Restaurant!"
Else
Msgbox "Go Home and Cook!"
End If
Ex:
If nAge = 0 Then
MsgBox "Welcome to the human race!"
ElseIf nAge < 0 Then
MsgBox "You have to grow up a bit before you start using VBScript!"
ElseIf nAge > 0 And Age < 10 Then
MsgBox "If you're bold enough, you must be old enough."
ElseIf nAge > 120 Then
MsgBox "You're getting too old for this stuff!"
Else
MsgBox "You're at the perfect age to get started!"
End If
Allows for conditional execution of a block of code, typically out of three or
more code blocks, based on some condition. Use the Select Case statement as
an alternative to complex nested If...Then...Else statements.
Select Case
Syntax
Select Case testexpression
[Case expressionlist-n
[statements-n]] ...
[Case Else
[elsestatements]]
End Select
A Select Case statement provides capability similar to the If...Then...Else
statement, but it makes code more efficient and readable. Select Case
structure is defined as follows:
Select Case expression
Case exp-1
...this is the code that executes if exp-1 matches expression
Case exp-2, exp-3
...this is the code that executes if exp-2 or exp-3 matches expression
Case exp-4
...this is the code that executes if exp-4 matches expression
.
.
.
Case Else
...this is the code that executes if none matches expression
End Select
Example
The following example uses Select Case to read a variable populated by the
user and determine the name of the user's operating system:
Select Case Left(Environment.Value("OSVersion"), 1)
Case 1 : varOSDesc = "Windows NT"
Case 2 : varOSDesc = "Windows 98"
Case 3 : varOSDesc = "Windows 95"
Case 4 : varOSDesc = "Windows 3.11"
Case 5 : varOSDesc = "Windows 2000"
Case 6 : varOSDesc = "Windows ME"
Case 7 : varOSDesc = "Windows XP"
Case Else : varOSDesc = "OS is unknown"
End Select
Using Control Structures to Make Code Repeat:
Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is False; others repeat statements until a condition is True. There are also loops that repeat statements a specific number of times.
The following looping statements are available in VBScript:
Do...Loop: Loops while or until a condition is True.
While...Wend: Loops while a condition is True.
For...Next: Uses a counter to run statements a specified number of times.
For Each...Next: Repeats a group of statements for each item in a collection or
each element of an array.
Using For...Next Statement:
Defines a loop that executes a given number of times, as determined by a loop counter. To use the For...Next loop, you must assign a numeric value to a counter variable. This counter is either incremented or decremented automatically with each iteration of the loop. In the For statement, you specify the value that is to be assigned to the counter initially and the maximum value the counter will reach for the block of code to be executed. The Next statement marks the end of the block of code that is to execute repeatedly, and also serves as a kind of flag that indicates the counter variable is to be modified.
Syntax
For counter = start To end [Step stepcounter]
[statements]
[Exit For]
[statements]
Next
Arguments
Argument Description counter
Numeric variable used as a loop counter. The variable can't be an array element or an element of a user-defined type.
start Initial value of counter.
end Final value of counter.
step
Amount counter is changed each time through the loop. If not specified, step defaults to one.
statements
One or more statements between For and Next that are executed the specified number of times.
No comments:
Post a Comment