Conditional Statements archives - The VBScript Network and Systems Administrator's Cafe

The VBScript Network and Systems Administrator's Cafe:

Conditional statements

Aug 9 2008   1:57AM GMT

VBScript Statements: Explanation of the While … Wend Statement



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

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


 

Jul 2 2008   12:06AM GMT

VBScript Statements: Explanation of the For Each … Next Statement



Posted by: Jerry Lees
VBScript Statements, vbscriptstatements, Conditional statements, working with arrays, for, next, for each, for each next

The For Each Next statement combination is very useful for situations where you need to loop through a piece of code a specific number of times, just like the For Next statement, except that you may not even know at run time how many items you need to loop through. 

This particular variation of the For Next statement is particularly helpful when iterating through arrays, objects, or collections– cases where you may not know how many items are in the object or collection with out going through them first. (Which would be a waste of resources)
 
You can use Exit For statements to exit out of a For Each Next loop, if you need to check for a condition inside the loop.

Here is an example of a For Each Next statement:

ReDim Collection(2)
Collection(0) = “Test”
Collection(1) = “Test 2″
Collection(2) = “Test 3″

For Each Item in Collection
     wscript.echo item
next


Jun 28 2008   11:20PM GMT

VBScript Statements: Explanation of the For … Next Statement



Posted by: Jerry Lees
VBScript, VBScript Statements, vbscriptstatements, Conditional statements, for, next, for next, step

The For Next statement is very useful for situations where you need to loop through a piece of code a specific number of times. 
 
You can use Exit For statements to exit out of a For loop, if you need to check for a condition inside the loop. Additionally, The Step keyword at the end of the line that has the for statement will allow you to loop through a For statement in any size of increment. 

Here is an example of a for statement, followed by one with a Step key word:

For x = 1 to 10
     wscript.echo x
next

For x=1 to 20 step 2
    wscript.echo x
next

 


Jun 14 2008   10:19PM GMT

VBScript Statements: Explanation of the Do Loop Statement



Posted by: Jerry Lees
VBScript, while, VBScript Statements, vbscriptstatements, do loop, until, do while, do until, 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)