The VBScript Network and Systems Administrator's Cafe

Jul 2 2008   12:06AM GMT

VBScript Statements: Explanation of the For Each … Next Statement

Jerry Lees Jerry Lees Profile: 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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: