The VBScript Network and Systems Administrator's Cafe:

for

Jul 2 2008   12:06AM GMT

VBScript Statements: Explanation of the For Each … Next Statement



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

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
for next, next, for, Conditional statements, VBScript, VBScript Statements, vbscriptstatements, 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