|
|
|
|
|
|
|
|
Lesson 4-Controlling Your VBScript RoutinesVBScript allows you to control how your scripts process data through the use of conditional and looping statements. By using conditional statements you can develop scripts that evaluate data and use criteria to determine what tasks to perform. Looping statements allow you to repetitively execute lines of a script. Each offers benefits to the script developer in the process of creating more complex and functional web pages.Conditional StatementsVBScript provides two forms of conditional statements: If..Then..Else Select..Case If..Then..ElseThe 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:
In this example statement the condition is:
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:
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:
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 If statement 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:
In this example when the condition is true, that is the customer's order is over $10,000, they receive a 10% discount. When the order is under $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:
In this example the customer receives a 10%discount for orders over $10000, a 5% discount for orders over $5000 and a handling fee of 3% for orders under $5000. As you see, VBScript offers you plenty of options when it comes to If statements. Select CaseThe 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:
For example, the following Select statement assigns different shipping fees based upon the State where the order is being sent:
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. NoteEven 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 Tutorial
Contents· What is VBScript? · How to Use this Tutorial Lesson 1 · Adding VBScript to Web Pages · The <SCRIPT> Tag · Non-Supporting Browsers · Your 1stirst VBScript Exercise Lesson 2 · Working with Variables · Declaring Variables · Scope of Variables · Constants · Arrays · Your 2nd VBScript Exercise Lesson 3 · Objects and VBScript · Adding Objects to Web Pages · Linking VBScript with Objects · Your 3rd VBScript Exercise Lesson 4 · Controlling VBScript Routines · Conditional Statements · Looping Statements · Your 4th VBScript Exercise Lesson 5 · Using VBScript with Forms · Validating Your Forms · Your 5th VBScript Exercise · Summary
|
Intranet Journal's Tutorials |
|
Managing Editor |