The VBScript Network and Systems Administrator's Cafe:

while

Aug 9 2008   1:57AM GMT

VBScript Statements: Explanation of the While … Wend Statement



Posted by: Jerry Lees
while wend, wend, Conditional statements, VBScript, VBScript Statements, while

The While conditional statement is useful because it allows you to repeat a block of code as long as a conditional test is True, rather than just a simple number of times. It is useful in situations where you need to loop through code, but do not know how many times yo need to run the portion of code.
 
Keep in mind that you must end While statements with a Wend statement.

Here is an example:

test =0
While test<100
     test = test +1
     Wscript.echo test
Wend


 

Jun 14 2008   10:19PM GMT

VBScript Statements: Explanation of the Do Loop Statement



Posted by: Jerry Lees
do until, do while, until, while, do loop, VBScript, VBScript Statements, vbscriptstatements, Conditional statements

The Do … Loop statement is very useful (like, for … next and Select … case) to execute a block of code more than once. However, where it differs from the other two is that the Do … Loop statement executes the code as long as (while) a condition is true or until a condition becomes true.

Typical uses of a Do … Loop are in cases where you may not know how many times you need to execute the code section because it could vary from situation to situation, like when reading in a file or taking in keyboard input.

In this example of a Do While Loop, we check to see if a variable is less than 10 and if not continue to run the code in the loop. (Notice how when x = 10 the code does not run and at the end x =10?)

x = 0
Do While x> 10
Wscript.Echo(”X = ” & x)
x= x + 1
Loop
Wscript.Echo(”X is now ” & x)

In this example of a Do Until Loop, we check to see if a variable is greater than 10, but use the until condition to run the loop. (Notice how when X = 10 the code does run and at the end x=11?)

x = 0
Do Until x > 10
Wscript.Wcho(”X = ” & x)
x= x + 1
Loop
Wscript.Echo(”X is now ” & x)