To learn more about author Sanjaya Hettihewa, please visit the author's homepage.
Operators, control structures, and iterative structures allow VBScript developers to create intelligent and interactive Web applications. The following topics are covered in this chapter:
Operators either assign the result of a certain calculation to a variable, or use the result to perform more calculations. VBScript supports several operators for various string, Boolean, and number-manipulation operations. The following sections demonstrate how operators are used in VBScript applications.
Syntax: <operand1> + <operand2>
The addition operator can be used to add two operands together. If both operands are numeric, the result of the addition operator is also numeric. However, if the operands are strings, VBScript performs a string concatenation instead of a numeric addition.
Note: To avoid ambiguity, use the string concatenation operator (&) when joining strings, and the addition operator (+) when adding numeric expressions.
Syntax: <operand1> - <operand2> Syntax: -<OperandToNegate>
The subtraction operator is used as the unary minus as well as the binary subtraction operator. When used as the binary subtraction operator, it subtracts <operand2> from <operand1> and returns the resulting value. When used as the unary minus, it negates the numeric operand it is used with.
Syntax: <operand1> * <operand2>
The multiplication operator takes two numeric operands, multiplies them, and returns the resulting value.
Syntax: <operand1> ^ <operand2>
The exponential operator returns the resulting value of <operand1> raised to the <operand2> power.
Syntax: <operand1> / <operand2>
The floating-point division operator is used to divide <operand1> by <operand2>. Both <operand1> and <operand2> have to be numeric expressions, and the resulting value is a floating-point number. Do not confuse the floating-point division operator with the integer-division operator. The result of 23/4 using the floating-point division operator is 5.75, while the result of 23\4 using the integer division operator is 5.
Syntax: <operand1> \ <operand2>
The integer-division operator is similar to the floating-point division operator. The integer-division operator returns an integer number after dividing <operand1> by <operand2>. If you want to experiment with this operator, see \Chap-16\Int.htm in the CD-ROM kit that accompanies this resource library. A few examples of the integer-division operator are listed next.
( 23 \ 4 ) = 5 ( 4 \ 23 ) = 0 ( 4 \ 2 ) = 2 ( 5 \ 2 ) = 2
Syntax: <operand1> & <operand2>
The string-concatenation operator can be used to join <operand1> and <operand2> together.
Syntax: <operand1> MOD <operand2>
The MOD operator is similar to the integer-division operator, except that it returns the remainder of <operand1> divided by <operand2>. If you want to experiment with this operator, see \Chap-16\Mod.htm in the CD-ROM kit that accompanies this resource library. A few examples of the MOD operator are listed next.
( 23 MOD 4 ) = 3 ( 4 MOD 23 ) = 4 ( 4 MOD 2 ) = 0 ( 5 MOD 2 ) = 1
VBScript supports a number of Boolean operators. The best way to explain how Boolean
operators work is with a truth table. See Figure 16.1 for truth tables of a number
of useful VBScript Boolean operators. VBScript Boolean operators are listed next,
along with how they can be used in VBScript programs.
Figure 16.1. Truth tables of VBScript
Boolean operators.
Syntax: <operand1> AND <operand2>
The AND operator returns TRUE if both <operand1> and <operand2> are true. If not, it returns FALSE. The AND operator can be used with expressions and functions that return a Boolean value.
Syntax: <operand1> OR <operand2>
The OR operator returns TRUE if either <operand1> or <operand2> is true. The OR operator can be used with expressions and functions that return a Boolean value.
Syntax: NOT <operand>
The NOT operator can be used to negate a Boolean value. The NOT operator can be used with expressions and functions that return a Boolean value.
Syntax: <operand1> XOR <operand2>
The XOR operator is very similar to the OR operator, except that in order for the XOR operator to return TRUE, <operand1> or <operand2> must be true. However, they both can't be true at the same time. The XOR operator can be used with expressions and functions that return a Boolean value.
Syntax: <operand1> Eqv <operand2>
The equivalence operator can be used to determine whether <operand1> is equal to <operand2>. If either <operand1> or <operand2> is NULL, the resulting value is also NULL. The truth table of the equivalence operator is listed here:
TRUE Eqv TRUE = TRUE FALSE Eqv TRUE = FALSE TRUE Eqv FALSE = FALSE FALSE Eqv FALSE = TRUE
(TRUE maybe replaced with binary 1 and FALSE maybe replaced with the binary 0.)
Syntax: <operand1> IS <operand2>
The object-reference operator is used to compare two object-reference variables. If <operand1> refers to the same object as <operand2>, the object-reference operator returns TRUE. Otherwise, it returns FALSE.
VBScript supports several comparison operators. Comparison operators can be used to compare strings as well as numbers. Comparison operators that can be used in VBScript programs are listed next.
Syntax: <operand1> = <operand2>
The equal operator returns TRUE if <operand1> and <operand2> are equal to each other. However, if either <operand1> or <operand2> is NULL, the equal operator returns NULL.
Syntax: <operand1> <> <operand2>
The unequal operator returns TRUE if <operand1> and <operand2> are unequal to each other. However, if either <operand1> or <operand2> is NULL, the unequal operator returns NULL.
Syntax: <operand1> < <operand2>
The less-than operator returns TRUE if <operand1> is less than <operand2>. However, if either <operand1> or <operand2> is NULL, the less-than operator returns NULL.
Syntax: <operand1> <= <operand2>
The less-than-or-equal-to operator returns TRUE if <operand1> is less than or equal to <operand2>. However, if either <operand1> or <operand2> is NULL, the less-than-or-equal-to operator returns NULL.
Syntax: <operand1> > <operand2>
The greater-than operator returns TRUE if <operand1> is greater than <operand2>. However, if either <operand1> or <operand2> is NULL, the greater-than operator returns NULL.
Syntax: <operand1> >= <operand2>
The greater-than-or-equal-to operator returns TRUE if <operand1> is greater than or equal to <operand2>. However, if either <operand1> or <operand2> is NULL, the greater-than-or-equal-to operator returns NULL.
Control structures are an important part of any programming language. They give a programming language life by allowing programmers to add intelligence to programs with the aid of conditional and iterative statements. A control structure transfers program flow to a series of VBScript statements or a VBScript subroutine based on a certain Boolean expression. The next few sections discuss how VBScript control structures are used in VBScript programs to transfer program flow.
Call is used to transfer program control to another VBScript subroutine. When Call is used to transfer control to subroutines having parameters, the parameters should be enclosed in parentheses. However, if Call is omitted, subroutine arguments do not need to be enclosed in parentheses. Return values of functions are ignored when they are invoked with the Call statement.
Note: The Call keyword is mentioned here because it is part of the VBScript language specification. The Call keyword is generally superfluous because it can be omitted when calling subroutines.
See Listing 16.1 for an example of how program control is transferred to other VBScript subroutines using the Call statement. Notice how the subroutine ThisSubroutineWillBeCalledWithCall, declared in lines 7-9, is called with the Call statement in line 20. See Figure 16.2 for the message box displayed when the VBScript application in Listing 16.1 is executed. The application in Listing 16.1 can be found in the CD-ROM kit that accompanies this resource library in the subdirectory /Chap-16/Call.html.
1: <HTML> 2: 3: <HEAD> 4: 5: <SCRIPT LANGUAGE="VBScript"> 6: <!-- 7: Sub ThisSubroutineWillBeCalledWithCall 8: MsgBox "Inside subroutine ThisSubroutineWillBeCalledWithCall" 9: end sub 10: --> 11: </SCRIPT> 12: 13: <TITLE>Transfering Program Control Using Call</TITLE> 14: </HEAD> 15: 16: <BODY BGCOLOR="FFFFFF"> 17: 18: <SCRIPT LANGUAGE="VBScript"> 19: <!-- 20: Call ThisSubroutineWillBeCalledWithCall 21: --> 22: </SCRIPT> 23: 24: </BODY> 25: </HTML>
Figure 16.2. Message box displayed when the application in Listing 16.1 is executed.
The Select...Case control structure is used to execute a series of VBScript statements based on an expression. The syntax of the Select...Case structure is as follows:
Select Case <ExpressionToTest> Case <ResultOfExpression> ... VBScript statement(s) ... Case Else ... VBScript statement(s) ... End Select
Listing 16.2 demonstrates how the Select...Case control structure is used to transfer program control to certain VBScript statements based on the value of an expression. When a user selects a color using the drop-down list shown in Figure 16.3, the Select...Case control structure in lines 26-40 (Listing 16.2) changes the background color of the Web page (as shown in Figure 16.4). The background color selector application in Listing 16.2 can be found in the CD-ROM kit that accompanies this resource library in the directory /Chap-16/Select.html.
1: <HTML> 2: <HEAD> 3: 4: <SCRIPT LANGUAGE="VBScript"> 5: <!-- 6: Sub window_onLoad() 7: BackgroundColorSelector.AddItem ("Red") 8: BackgroundColorSelector.AddItem ("Blue") 9: BackgroundColorSelector.AddItem ("Yellow") 10: BackgroundColorSelector.AddItem ("Black") 11: BackgroundColorSelector.AddItem ("White") 12: end sub 13: --> 14: </SCRIPT> 15: 16: <TITLE>Transfering Program Control Using SELECT / CASE</TITLE> 17: </HEAD> 18: <BODY BGCOLOR="FFFFFF"> 19: <B> 20: Please use the pull down menu to select the background color ¬of this Web page. 21: </B> 22: 23: <SCRIPT LANGUAGE="VBScript"> 24: <!-- 25: Sub BackgroundColorSelector_Change() 26: Select Case BackgroundColorSelector.Value 27: Case "Red" 28: Document.bgColor = "Red" 29: Case "Blue" 30: Document.bgColor = "Blue" 31: Case "Yellow" 32: Document.bgColor = "Yellow" 33: Case "Black" 34: Document.bgColor = "Black" 35: Case "White" 36: Document.bgColor = "White" 37: Case Else 38: MsgBox "Please select a a valid color!" & 39: document.ColorSelectorForm.BackgroundColorSelector.text 40: End Select 41: end sub 42: --> 43: </SCRIPT> 44: 45: <OBJECT ID="BackgroundColorSelector" WIDTH=269 HEIGHT=21 46: CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002F3"> 47: <PARAM NAME="VariousPropertyBits" VALUE="746604571"> 48: <PARAM NAME="BackColor" VALUE="14417394"> 49: <PARAM NAME="DisplayStyle" VALUE="3"> 50: <PARAM NAME="Size" VALUE="7112;556"> 51: <PARAM NAME="MatchEntry" VALUE="1"> 52: <PARAM NAME="ShowDropButtonWhen" VALUE="2"> 53: <PARAM NAME="FontCharSet" VALUE="0"> 54: <PARAM NAME="FontPitchAndFamily" VALUE="2"> 55: </OBJECT> 56: 57: </BODY> 58: </HTML>
Figure 16.3. selecting a background color using the pull-down menu.
Figure 16.4. The VBScript application changes the background color of the Web page.
Tip: It is a good programming practice to include a Case Else statement in every Select...Case statement. Failure to do so can result in applications that behave erratically as a result of unanticipated circumstances. Try to think of all possible cases and structure your Select...Case statements to handle each case properly.
Note: A Select...Case structure can be nested inside another Select...Case struc- ture, provided that each Select...Case structure has a matching End Select statement.
The IF...THEN statement is used to execute certain VBScript statements based on a Boolean expression. The syntax of the IF...THEN control structure is as follows:
IF <BooleanExpression> THEN ... VBScript statement ... END IF
As shown in the previous example, an IF...THEN control structure can execute certain VBScript statements based on the value of a Boolean expression.
The IF...THEN...ELSE statement can be used to execute certain VBScript statements based on Boolean expressions. The syntax of the IF...THEN...ELSE control structure is as follows:
IF <BooleanExpression> THEN ... VBScript statement ... ELSE IF <BooleanExpression> THEN ... VBScript statement ... ELSE ... VBScript statement ... END IF
As shown in the previous example, an IF...THEN...ELSE statement can execute certain VBScript statements based on the values of various Boolean expressions.
You might be wondering when you should use an IF...THEN...ELSE control structure and when you should use a Select...Case control structure. Use a Select...Case control structure if certain VBScript statements have to be executed based on the value of a single expression. Use an IF...THEN...ELSE control structure if certain VBScript statements have to be executed based on the values of different Boolean expressions.
For example, if subroutine ALessThanB() has to be executed if the variable A is less than B, the subroutine AGreaterThanB() has to be executed if the variable A is greater than B, and the subroutine AEqualsB() has to be executed if A equals B, use an IF...THEN...ELSE control structure. On the other hand, if certain VBScript statements have to be executed based on the value of a single expression (the value of a string variable, for example), use a Select...Case control structure. For example, a Select...Case control structure is ideal to execute the subroutine Red() if the variable VariableColor equals Red and execute the subroutine Blue() if the variable VariableColor equals Blue, and execute the subroutine Green() if the variable VariableColor equals Green.
When developing applications, it is sometimes necessary to perform a series of tasks more than once. VBScript provides several iterative structures that can be used to repeatedly perform a series of actions. There are two kinds of iterative structures--those that repeat until a certain condition is met, and those that repeat for a certain number of iterations.
Warning: Be careful when using iterative structures that repeat until a certain condition is met. Otherwise, you can inadvertently create an infinite loop that iterates forever. Always choose well-defined starting and ending conditions when using this type of iterative structure.
The For...Next control structure can be used to iterate a group of VBScript statements a certain number of times. The syntax of the For...Next control structure is as follows:
For <LoopCount> = <BeginLoop> To <EndLoop> Step <StepCount> ... VBScript statements ... Next
The previous definition can be used to iterate a group of VBScript statements a certain number of times by replacing certain labels (enclosed in pointed braces) of the definition:
Note: The Exit...For statement can be used to exit a For loop.
The application in Listing 16.3 demonstrates how the For...Next control structure is used to iterate a series of VBScript statements. In this case, the For...Next control does this by printing all even numbers between 0 and 20, and printing an asterisk for each number. The output of the application in Listing 16.3 can be found in Figure 16.5. Notice in Listing 16.3 how the For...Next loop found in lines 16-18 is nested inside the For...Next loop found in lines 14-20. The source code of the application shown in Listing 16.3 can be found in the CD-ROM kit that accompanies this resource library in the directory /Chap-16/ForLoop.html.
1: <HTML> 2: <HEAD> 3: <TITLE>Using the FOR / NEXT control structure</TITLE> 4: </HEAD> 5: <BODY BGCOLOR="FFFFFF"> 6: <H3> 7: Printing even numbers from 0 to 20 using the FOR / NEXT 8: control structure and printing a histograph of the numbers 9: using asterisks. 10: </H3> 11: <SCRIPT LANGUAGE="VBScript"> 12: <!-- 13: 14: For LoopCountVariable = 0 To 20 Step 2 15: document.write (LoopCountVariable-1) 16: For AsterisksCountVariable = 0 To LoopCountVariable 17: document.write "*" 18: Next 19: document.write "<BR>" 20: Next 21: --> 22: </SCRIPT> 23: 24: </BODY> 25: </HTML>
Figure 16.5. The For...Next control structure is used to iterate a series of VBScript statements.
The For Each...Next control structure is useful for iterating VBScript statements for each object in a collection or each element in an array. The syntax of the For Each...Next loop is as follows:
For Each <LoopIndex> In <ArrayOrCollection> ... VBScript statements ... Next <LoopIndex>
A For Each...Next loop can be added to a VBScript program by substituting certain labels of the preceding example as follows:
The Exit For statement can be used to exit a For Each loop. Also note that <LoopIndex> can be omitted in the Next <LoopIndex> statement. However, this is not recommended; it can complicate things and cause errors if a For Each loop is nested inside another For Each loop.
The While...Wend control structure can be used to iterate a group of VBScript statements while a certain Boolean expression is true. See Chapter 23, "Developing VBScript Applications," for some practical applications of the While...Wend control structure. The syntax of the While...Wend control structure is as follows:
While <BooleanExpression> ... VBScript statements ... Wend
Listing 16.4 demonstrates how the While...Wend control structure is used to iterate a series of VBScript statements. The source code of the application shown in Listing 16.4 can be found in the CD-ROM kit that accompanies this resource library in the directory /Chap-16/While.html. Notice how the loop control variable has to be manually incremented for each While...Wend control structure (lines 21 and 25). See Figure 16.6 for the output of the VBScript application in Listing 16.4.
1: <HTML> 2: <HEAD> 3: <TITLE>Using the While/Wend control structure</TITLE> 4: </HEAD> 5: <BODY BGCOLOR="FFFFFF"> 6: <H3> 7: Printing even numbers from 0 to 20 using the While/Wend control structure 8: and printing a histograph of the numbers using asterisks.</H3> 9: <SCRIPT LANGUAGE="VBScript"> 10: <!-- 11: 12: Dim LoopCountVariable, AsterisksCountVariable 13: 14: LoopCountVariable = 0 15: While LoopCountVariable <= 20 16: If ( LoopCountVariable MOD 2 = 0 ) Then 17: document.write LoopCountVariable 18: AsterisksCountVariable = 0 19: While AsterisksCountVariable < LoopCountVariable 20: document.write "*" 21: AsterisksCountVariable = AsterisksCountVariable + 1 22: Wend 23: document.write "<BR>" 24: End If 25: LoopCountVariable = LoopCountVariable + 1 26: Wend 27: 28: --> 29: </SCRIPT> 30: 31: </BODY> 32: </HTML>
Figure 16.6. The While...Wend control structure is used to iterate a series of VBScript statements.
The Do...Loop control structure can be used to iterate a group of statements until a certain Boolean expression becomes TRUE. The syntax of the Do...Loop control structure is listed next. As shown in the following examples, the Boolean expression of a Do...Loop structure can be placed either at the beginning or the end of the control structure (in the first example, the Boolean expression appears at the beginning of the control structure).
Do <condition> <BooleanExpression> ... VBScript statements ... Loop
In this example, the Boolean expression is placed at the end of the control structure.
Do ... VBScript statements ... Loop <condition> <BooleanExpression>
The preceding examples repeatedly execute VBScript statements enclosed in the loop structure until <BooleanExpression> becomes true. In the examples, <condition> may be replaced with either While or Until. As the name implies, if While is used the loop iterates while <BooleanExpression> is TRUE. Likewise, if Until is used the loop iterates until <BooleanExpression> is TRUE.
Listing 16.5 demonstrates how the Do...While/Until...Loop control structure is used to iterate a series of VBScript statements. See Figure 16.7 for the output of Listing 16.5. The source code of the application shown in Listing 16.5 can be found in the CD-ROM kit that accompanies this resource library in the directory /Chap-16/DoWhile.html.
1: <HTML> 2: <HEAD> 3: <TITLE>Using the Do/While/Until/Loop control structure</TITLE> 4: </HEAD> 5: <BODY BGCOLOR="FFFFFF"> 6: <H3> 7: Printing even numbers from 0 to 20 using the ¬Do/While/Until/Loop control structure 8: and printing a histograph of the numbers using asterisks.</H3> 9: <SCRIPT LANGUAGE="VBScript"> 10: <!-- 11: 12: Dim LoopCountVariable, AsterisksCountVariable 13: 14: LoopCountVariable = 0 15: 16: Do 17: If ( LoopCountVariable MOD 2 = 0 ) Then 18: document.write LoopCountVariable 19: AsterisksCountVariable = 0 20: Do While AsterisksCountVariable < LoopCountVariable 21: document.write "*" 22: AsterisksCountVariable = AsterisksCountVariable + 1 23: Loop 24: document.write "<BR>" 25: End If 26: LoopCountVariable = LoopCountVariable + 1 27: Loop While LoopCountVariable <= 20 28: 29: --> 30: </SCRIPT> 31: 32: </BODY> 33: </HTML>
Figure 16.7. The Do...While/Until...Loop control structure is used to iterate a series of VBScript statements.
The Exit statement causes program control to be transferred out of the control structure in which it is used. The control structure can be a loop or a subroutine. Forms of the Exit command are listed next.
While operators are used to perform certain calculations, control structures and iterative structures are used to transfer program flow and perform various tasks depending on certain Boolean expressions.
© Copyright, Macmillan Computer Publishing. All rights reserved.