WE have four looping statements in VBScript
For...Next
1)
Loop #0
Loop #1
Loop #2
Loop #3
With the help of Step keyword, we can increase or decrease the counter by the value specified.
1)
For i=2 To 8 Step 2
any code
Next
2)
For i=8 To 2 Step -2
any code
Next
For Each...Next statement
It is useful when we don’t know how many elements are there in the array.
1)
2)
dim names(2)
names(0)="happy"
names(1)="Sumit"
names(2)="Love"
For Each x in names
document.write(x & "
")
Next
Do...Loop
It will repeat a block of code while a condition is True or until a condition becomes True
1)
Do While i>9
some code
Loop
If i equals 8, the code inside the loop above will never be executed.
2)
Do
some code
Loop While i>9
The code inside this loop will be executed at least one time, even if i is less than 9.
Repeating Code Until a Condition Becomes True
1)
Do Until i=9
some code
Loop
If i equals 9, the code inside the loop will never be executed.
2)
Do
some code
Loop Until i=9
The code inside this loop will be executed at least one time, even if i is equal to 9.
The Exit statement can only be used within a Do...Loop control structure to
provide an alternate way to exit a Do...Loop.
we must end all Do statements with Loop or otherwise error message will pop up. The While and the Until condition may be placed after the Do or the Loop.
Some Examples:
1)
num = 1
Do
num = num + 1
Loop Until num = 5
2)
num = 1Do While num < 5
num = num + 1
Loop
3)
num = 1
Do
num = num + 1 br>Loop While num < 5
Exit a Do...Loop
You can exit a Do...Loop statement with the Exit Do keyword.
1)
Do Until i=9
i=i-1
If i<9 Then Exit Do
Loop
The code inside this loop will be executed as long as i is different from 9, and as long as i is greater than 9.
While...Wend statement
While Loop is a simple loop that keeps looping while a condition is true
1)
9
8
7
6
5
4
3
2
1
Finish!
Please go here for 10 VBScript techniques
http://www.windowsitpro.com/Articles/ArticleID/20979/20979.html?Ad=1
In my June 2001 column, I shared 10 basic VBScript techniques. For those who want to step up a level and begin writing productive administrative scripts, here are 10 more VBScript techniques.
10. On Error—The On Error statement lets a script trap runtime errors and continue executing. You
can test for errors in the script after each statement has executed.
On Error Resume Next
9. InStr—This function lets you locate a substring in a string. The function returns the starting position of the substring or a 0 if the function doesn't find the string. In
nPos = InStr("123345", "33")
nPos has a value of 3 because "33" begins in the third position of "123345."
8. The Do Loop—This basic mechanism for repeatedly executing a set of statements comes in two forms: a Do Until Loop and a Do While Loop. The most important distinction between
the two loops is that the Do Until Loop always executes at least once.
Do Until myValue > 1
myValue = myValue + 1
Loop
7. Subroutines—Modularizing your code into subroutines lets you organize your scripts and create reusable routines. You can define subroutines anywhere in a script. You use subroutines when you don't need to return a value to the calling code. . . .