Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Testing Conditions in VBScript

VBScript provides one control structure for making decisions—the If…Then…Else structure. To make a decision, you supply one or more expressions that evaluate to true or false; which code is executed depends on what your expressions evaluate to.

The simplest form of If…Then…Else uses only the If…Then part. If the specified condition is true, the code following the condition is executed; if not, that code is skipped. In the following code fragment, for example, the message appears only if the variable x is less than pi:

if (x < pi) then document.write(“x is less than pi”)

You can use any expression as the condition. Because you can nest and combine expressions with the logical operators, your tests can be pretty sophisticated. Also, using the multiple statement character, you can execute multiple commands, as in the following:

if ((test = TRUE) And (x > max)) then max = x : test = FALSE

The else clause enables you to specify a set of statements to execute when the condition is false. In the same single line form shown in the preceding line, your new line appears as follows:

if (x > pi) then test = TRUE else test = FALSE

A more versatile use of the If…Then…Else allows multiple lines and multiple actions for each case. It looks something like the following:

if (x > pi) then
    test = TRUE
    count = count + 1
else
    test = FALSE
    count = 0
end if

Note that with this syntax, additional test clauses using the elseif statement are permitted. You could, for example, add one more clause to the preceding example:

if (x > pi) then
    test = TRUE
    count = count + 1
elseif (x < -pi) then
    test = TRUE
    count = count - 1
else
    test = FALSE
    count = 0
end if

Executing VBScript Loops

If you want to repeat an action more than one time, VBScript provides a variety of constructs for doing so. The first, called a For…Next loop, executes a set of statements some number of times. You specify three expressions: an initial expression, which sets the values of any variables you need to use; a final value, which tells the loop how to see when it is done; and an increment expression, which modifies any variables that need it. The following is a simple example:

for count = 0 to 100 step 2
    document.write(“Count is “ & CStr(count) & “<BR>”)
next

In this example, the expressions are all simple numeric values—the initial value is 0, the final value is 100, and the increment is 2. This loop executes 51 times and prints out a number each time.

The third form of loop is the While…Wend loop. It executes statements as long as its condition is true. You can rewrite the first For…Next loop, for example, as follows:

count = 0
while (count <= 100)
    document.write(“Count is “ & CStr(count) & “<BR>”)
    count=count + 2
wend

The last type of loop is the Do…Loop, which has several forms, and which either test the condition at the beginning or the end. The test can either be a Do While or Do Until, and can occur at the beginning or end of the loop. If a Do While test is done at the beginning, the loop executes as long as the test condition is true, similar to the While…Wend loop. The following is an example:

count = 0
do while (count <= 100)
    document.write(“Count is “ & CStr(count) & “<BR>”)
    count = count + 2
loop

An example of having the test at the end, as a Do…Until, can also yield equivalent results. In that case, the loop looks like the following:

count = 0
do
    document.write(“Count is “ & CStr(count) & “<BR>”)
    count = count + 2
loop until (count > 100)

One other difference between these two forms is that when the test is at the end of the loop, as in the second case, the commands in the loop are executed at least one time. If the test is at the beginning, that is not the case.

Which form you prefer depends on what you are doing. For…Next loops prove useful when you want to perform an action a set number of times. While…Wend and Do…Loop loops, although they can be used for the same purpose, are best when you want to keep doing something as long as a particular condition remains true.


NOTE:  The For…Next and Do…Loop loops also have a way to exit the loop from inside—the End For and End Do statements, respectively. Normally, these tests would be used as part of a conditional statement, such as the following:
for i=0 to 100
    x=UserFunc()
    document.write(“x[“ & CStr(i) & “]=“ & CStr(x) & “<BR>”)
    if (x > max) end for
next

Using Other VBScript Statements

This section provides a quick reference to some of the other VBScript statements. The following formatting is used:

  All VBScript keywords are in a monospace font.
  Words in monospace italic represent user-defined names or statements.
  Any portions enclosed in square brackets ([ and ]) are optional.
  Portions enclosed in braces ({ and }) and separated by a vertical bar (|) represent an option, of which one must be selected.
  The word statements… indicates a block of one or more statements.

The Call Statement

The Call statement calls a VBScript Sub or Function procedure as follows:

Syntax:

Call MyProc([arglist])

or

MyProc [arglist]

Note that arglist is a comma-delimited list of zero or more arguments to be passed to the procedure. When the second form is used, omitting the Call statement, the parentheses around the argument list, if any, must also be omitted.

The Dim Statement

The Dim statement is used to declare variables and also to allocate the storage necessary for them. If you specify subscripts, you can also create arrays.

Syntax:

Dim varname[([subscripts])][,varname[([subscripts])],...]

The Function and Sub Statements

The Function and Sub statements declare VBScript procedures. The difference is that a Function procedure returns a value, and a Sub procedure does not. All parameters are passed to functions by value—the function gets the value of the parameter but cannot change the original value in the caller.

Syntax:

[Static] Function funcname([arglist])
    statements...
    funcname=returnvalue
End

and

[Static] Sub subname([arglist])
    statements...
End


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.