Conditional Constructs execute statements or repeat certain set of statements based on conditions.
The following conditional constructs are available in VBScript
· If – Then –Else
· Select Case
saved bymikeem em on 2008-02-11
The If – Then- Else Construct is used to evaluate whether a condition is true or false and depending on the result, to specify one or more statements to execute. Usually the condition is an expression that uses a comparison operator to compare one value or variable with another. The If- Then – Else statements can be nested to as many levels as needed.
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 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.
Select Case Construct
The Select-Case structure is an alternative to If Then Else for selectively executing one block of statements from among multiple blocks of statements. The Select Case Construct makes code more efficient and readable.
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.
For example:
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
Iterative Constructs
Looping allows to run a group of statements repeatedly. The loop is repeated based on a condition. The loop runs as long as the condition is true. The following looping constructs are available in VBScript.· Do – Loop
· While – Wend
· For – Next
The basic difference between a “Do while – Loop” and “Do - Loop while” is that the previous one gets executed only when the condition in the while statement holds true where as a “Do – Loop while” gets executed atleast once, because the condition in the while statement gets checked at the end of the first iteration.
For example:
Dim j, totalFor j = 2 To 10 Step 2
total = total + j
Next
MsgBox "The total is " & total Arrays
An array is a contiguous area in the memory referred to by a common name. It is a series of variables having the same data type. Arrays are used to store related data values. VBScript allows you to store a group of common values together in the same location. These values can be accessed with their reference numbers.
An array is made up of two parts, the array name and the array subscript. The subscript indicates the highest index value for the elements within the array. Each element of an array has a unique identifying index number by which it can be referenced. VBScript creates zero based arrays where the first element of the array has an index value of zero.
· Local Arrays
A local array is available only within the function or procedure, where it is declared.
· Global Arrays
A global array is an array that can be used by all functions and procedures. It is declared at the beginning of the VBScript Code.
The Dim statement is used to declare arrays. The syntax for declaring an array is as follows:
Dim ArrayName(subscriptvalue)
Where, ArrayName is the unique name for the array and SubscriptValue is a numeric value that indicates the number of elements in the array dimension within the array.
Example:
Dim No_Passengers(3)
The No_Passengers can store 4 values.
No_Passengers(1) = 2
No_Passengers(2) = 3
No_Passengers(3) = 4
Static and Dynamic Arrays:VBScript provides flexibility for declaring arrays as static or dynamic.
A static array has a specific number of elements. The size of a static array cannot be altered at run time.
A dynamic array can be resized at any time. Dynamic arrays are useful when size of the array cannot be determined. The array size can be changed at run time.
Next we will deal with user defined procedures, functions and subroutines.