| VBScript conditional and looping |
|
| Conditional Statements |
|
| VBScript provides two forms of conditional statements: |
|
If..Then..Else
Select..Case |
|
| If..Then..Else |
|
| The If..Then..Else statement is used, first to evaluate a condition to see if it is true or false and second, depending upon the condition, to execute a statement or set of statements. Rather than discussing an If statement in theory, we will examine some examples to see how they work. |
|
| The simplest version of an If statement is one that contains only a condition and a single statement: |
|
If AmountPurchased > 10000 Then
DiscountAmount = AmountPurchased * .10
In this example statement the condition is:
If AmountPurchased > 10000 |
|
| Which simply checks to see if the contents of the variable AmountPurchased is greater than ten thousand. If it is, the condition is true. In this simple version of the If statement when the condition is true the following statement is executed: |
|
| DiscountAmount = AmountPurchased * .10 |
|
| Next we will look at a more complicated version of the If statement. In this version we will perform a series of statements when the condition is true: |
|
| If Amount Purchased > 10000 Then |
|
| DiscountAmount = AmountPurchased * .10 |
|
| Subtotal = AmountPurchased - DiscountAmount |
|
| End If |
|
| In this form of the If statement, one or more statements can be executed when the condition is true, by placing them between the If statement on top and the End Ifstatement on the bottom. |
|
| The next form of the If statement uses the If..Then..Else format. This version of the If statement differs from the two previous versions in that it will perform one set of statements if the condition is true and another set when the condition is false: |
|
If AmountPurchased > 10000 Then
DiscountAmount = AmountPurchased * .10
Subtotal = AmountPurchased - DiscountAmount
Else
HandlingFee = AmountPurchased *.03
Subtotal = AmountPurchased + HandlingFee
End If |
|
| In this example when the condition is true, that is the customer's order is over Rs 10,000, they receive a 10% discount. When the order is under Rs 10,000, they are charged a 3% handling fee. |
|
| The final version of the If statement that we will look at is the If..Then..Else If. In this form the If statement checks each of the conditions until it either finds one that is true or an Else statement: |
|
If AmountPurchased > 10000 Then
DiscountAmount = AmountPurchased * .10
Subtotal = AmountPurchased - DiscountAmount
Else If AmountPurchased > 5000 Then
DiscountAmount = AmountPurchased * .05
Subtotal = AmountPurchased - DiscountAmount
Else
HandlingFee = AmountPurchased *.03
Subtotal = AmountPurchased + HandlingFee
End If |
|
| In this example the customer receives a 10%discount for orders over Rs 10000, a 5% discount for orders over Rs 5000 and a handling fee of 3% for orders under Rs 5000. |
|
| As you see, VBScript offers you plenty of options when it comes to If statements. |
|
| Select Case |
|
| The Select Case statement provides an alternative to the If..Then..Else statement, providing additional control and readability when evaluating complex conditions. |
|
| It is well suited for situations where there are a number of possible conditions for the value being checked. Like the If statement the Select Case structure checks a condition, and based upon that condition being true, executes a series of statements |
|
| The syntax of the Select Case statement is: |
|
Select Case condition
Case value
Case value
...Case Else
End Select |
|
| For example, the following Select statement assigns different shipping fees based upon the State where the order is being sent: |
|
Select Case Document.frmOrder.txtState.Value
Case "Kolkata"
ShippingFee= .04
Case "Bombay"
ShippingFee = .03
Case Else
ShippingFee = .02
End Select |
|
| The Select Case statement checks each of the Case statements until it finds one that will result in the condition being true. If none are found to be true, it executes the statements within the Case Else. |
|
| Note |
|
| Even though it is not required, always include a Case Else when working with Select Case statements to process conditions that you may not have considered possible. |
|
| For these conditions you can display something as simple as a message dialog to inform you that a branch was executed that you hadn't planned for. |
|
|
|
|
|
|
| VBScript conditional and looping |
|
| Looping Statements |
|
| Very often when you write code, you want to allow the same block of code to run a number of times. You can use looping statements in your code to do this. |
|
| In VBScript we have four looping statements: |
|
• For...Next statement - runs statements a specified number of times.
• For Each...Next statement - runs statements for each item in a collection or each element of an array
• Do...Loop statement - loops while or until a condition is true
• While...Wend statement - Do not use it - use the Do...Loop statement instead |
|
| For...Next Loop |
|
| You can use a For...Next statement to run a block of code, when you know how many repetitions you want. |
|
| You can use a counter variable that increases or decreases with each repetition of the loop, like this: |
|
For i=1 to 10
some code
Next |
|
| The For statement specifies the counter variable (i) and its start and end values. The Next statement increases the counter variable (i) by one. |
|
| Step Keyword |
|
| Using the Step keyword, you can increase or decrease the counter variable by the value you specify. |
|
| In the example below, the counter variable (i) is increased by two each time the loop repeats. |
|
For i=2 To 10 Step 2
some code
Next |
|
| To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value. |
|
| In the example below, the counter variable (i) is decreased by two each time the loop repeats. |
|
For i=10 To 2 Step -2
some code
Next |
|
| Exit a For...Next |
|
| You can exit a For...Next statement with the Exit For keyword. |
|
| For Each...Next Loop |
|
| A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array. |
|
dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
For Each x in cars
document.write(x & "<br />")
Next |
|
| Do...Loop |
|
| You can use Do...Loop statements to run a block of code when you do not know how many repetitions you want. The block of code is repeated while a condition is true or until a condition becomes true. |
|
| Repeating Code While a Condition is True |
|
You use the While keyword to check a condition in a Do...Loop statement.
Do While i>10
some code
Loop
If i equals 9, the code inside the loop above will never be executed.
Do
some code
Loop While i>10 |
|
| The code inside this loop will be executed at least one time, even if i is less than 10. |
|
| Repeating Code Until a Condition Becomes True |
|
| You use the Until keyword to check a condition in a Do...Loop statement. |
|
| Do Until i=10 |
|
| some code |
|
| Loop |
|
| If i equals 10, the code inside the loop will never be executed. |
|
| Do |
|
| some code |
|
| Loop Until i=10 |
|
| The code inside this loop will be executed at least one time, even if i is equal to 10. |
|
| Exit a Do...Loop |
|
| You can exit a Do...Loop statement with the Exit Do keyword. |
|
| Do Until i=10 |
|
| i=i-1 |
|
| If i<10 Then Exit Do |
|
| Loop |
|
| The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10. |
|
|
|
|
|
|
| VBScript Procedures |
|
| Introduction |
|
| We have two kinds of procedures: The Sub procedure and the Function procedure. |
|
| A Sub procedure: |
|
• is a series of statements, enclosed by the Sub and End Sub statements
• can perform actions, but does not return a value
• can take arguments that are passed to it by a calling procedure
• without arguments, must include an empty set of parentheses () |
|
Sub mysub()
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub |
|
| A Function procedure: |
|
• is a series of statements, enclosed by the Function and End Function statements
• can perform actions and can return a value
• can take arguments that are passed to it by a calling procedure
• without arguments, must include an empty set of parentheses ()
• returns a value by assigning a value to its name |
|
Function myfunction()
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function |
|
|
|
|
|
|
| VBScript Procedures |
|
| Sub Procedures |
|
| A Sub procedure is a series of VBScript statements, enclosed by Sub and End Substatements, that perform actions but don't return a value. |
|
| A Sub procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). |
|
| If a Sub procedure has no arguments, its Sub statement must include an empty set of parentheses (). |
|
| The following Sub procedure uses two intrinsic, or built-in, VBScript functions, MsgBoxand InputBox, to prompt a user for some information. It then displays the results of a calculation based on that information. |
|
| Description |
|
| Declares the name, arguments, and code that form the body of a Sub procedure. |
|
Syntax
[Public | Private] Sub name [(arglist)]
[statements]
[Exit Sub] [statements]
End Sub |
|
| Example: |
|
| Sub ConvertTemp() |
|
| temp = InputBox("Please enter the temperature in degrees F.", 1) |
|
| MsgBox "The temperature is " & Celsius(temp) & " degrees C." |
|
| End Sub |
|
| The Sub statement syntax has these parts: |
|
| Part | Description |
| Public | Indicates that the Sub procedure is accessible to all other procedures in all scripts. |
| Private | Indicates that the Sub procedure is accessible only to other procedures in the script where it is declared. |
| name | Name of the Sub; follows standard variable naming conventions. |
| arglist | List of variables representing arguments that are passed to the Sub procedure when it is called. Multiple variables are separated by commas. |
| statements | Any group of statements to be executed within the body of the Sub procedure. |
|
|
| The arglist argument has the following syntax and parts: |
|
| [ByVal | ByVal] varname[( )] |
|
| Part | Description |
| ByVal | Indicates that the argument is passed by value. |
| ByRef | Indicates that the argument is passed by reference. |
| varname | Name of the variable representing the argument; follows standard variable naming conventions. |
|
|
|
|
|
|
|
| VBScript Procedures |
|
| Function Procedures |
|
| A Function procedure is a series of VBScript statements enclosed by the Functionand End Function statements. |
|
| A Function procedure is similar to a Sub procedure, but can also return a value. AFunction procedure can take arguments (constants, variables, or expressions that are passed to it by a calling procedure). |
|
| If a Function procedure has no arguments, its Function statement must include an empty set of parentheses. |
|
| A Function returns a value by assigning a value to its name in one or more statements of the procedure. The return type of a Function is always a Variant. |
|
| Description |
|
| Declares the name, arguments, and code that form the body of a Function procedure. |
|
Syntax
[Public | Private] Function name [(arglist)]
[statements]
[name = expression]
[Exit Function]
[statements]
[name = expression]
End Function |
|
| In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. When the function is called from the Convert Temp Sub procedure, a variable containing the argument value is passed to the function. |
|
| The result of the calculation is returned to the calling procedure and displayed in a message box. |
|
| Sub ConvertTemp() |
|
| temp = InputBox("Please enter the temperature in degrees F.", 1) |
|
| MsgBox "The temperature is " & Celsius(temp) & " degrees C." |
|
| End Sub |
|
| Function Celsius(fDegrees) |
|
| Celsius = (fDegrees - 32) * 5 / 9 |
|
| End Function |
|
| The Function statement syntax has these parts: |
|
| Part | Description |
| Public | Indicates that the Function procedure is accessible to all other procedures in all scripts. |
| Private | Indicates that the Function procedure is accessible only to other procedures in the script where it is declared. |
| name | Name of the Function; follows standard variable naming conventions |
| arglist | List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas. |
| statements | Any group of statements to be executed within the body of the Function procedure |
| expression | Return value of the Function. |
|
|
| The arglist argument has the following syntax and parts: |
|
| [ByVal | ByVal] varname[( )] |
|
| Part | Description |
| ByVal | Indicates that the argument is passed by value. |
| ByRef | Indicates that the argument is passed by reference. |
| varname | Name of the variable representing the argument; follows standard variable naming conventions. |
|
|
|
|
|
|
|
| VBScript Procedures |
|
| Getting Data into and out of Procedures |
|
| Each piece of data is passed into your procedures using an argument. Arguments serve as placeholders for the data you want to pass into your procedure. |
|
| You can name your arguments anything that is valid as a variable name. When you create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure. |
|
| Any arguments are placed inside these parentheses, separated by commas. For example, in the following example, fDegrees is a placeholder for the value being passed into the Celsius function for conversion: |
|
| Function Celsius(fDegrees) |
|
| Celsius = (fDegrees - 32) * 5 / 9 |
|
| End Function |
|
| To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure can't. |
|
| Using Sub and Function Procedures in Code |
|
| A Function in your code must always be used on the right side of a variable assignment or in an expression. |
|
| For example: |
|
Temp = Celsius(fDegrees)
or
MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees." |
|
| To call a Sub procedure from another procedure, you can just type the name of the procedure along with values for any required arguments, each separated by a comma. |
|
| The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses. |
|
| The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing. |
|
| Call MyProc(firstarg, secondarg) |
|
| MyProc firstarg, secondarg |
|
| Notice that the parentheses are omitted in the call when the Call statement isn't used. |
|
|
|
|
|
|
|
|
|
|
|
|
0 comments: