VBScript Statements: Explanation of the For Each … Next Statement
Posted by: Jerry Lees
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



You must be logged-in to post a comment. Log-in/Register