A for loop is used when you need to repeat code a certain amount of times. It also can be used in objects like collections.
For To LoopsEdit
The for loop is structured like this
For i = 1 To 20 MsgBox "I have annoyed you " & i & " times! :D" Next i
It could be shown like this
i = 1 While i <= 20 MsgBox "I have annoyed you " & i & " times! :D" i = i + 1 Wend
- i is the variable that is used.
- 1 is the start value to set
- 20 is the value that it will attempt to get to.
For i = 20 To 1 Step -1 MsgBox "I will annoy you " & i & " more times! Mwuhahaha!" Next i
- The Step statement can be used to skip a certain amount of times instead of 1, so -1 decreases the value
For Each LoopsEdit
For Each vVariant In cCollection MsgBox vVariant Next
It means vVariant will be set to each item in the collection. This works only with loop counters that are of Variant or of any object type (either Object or something more specific).
- In VBScript, you are required to use "Next" and not "Next i" etc.
|If | For | While | Do | Until|