VBScript follow sequential execution of statement, however sometime we need LOOP statement to fulfill the requirement to execute a set of statements several number of times.
'for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.'for ..each loop This is executed if there is at least one element in group and reiterated for each element in a group.
'while..wend loop This tests the condition before executing the loop body.
'do..while loops The do..While statements will be executed as long as condition is True.(i.e.,) The Loop should be repeated till the condition is False.
'do..until loops The do..Until statements will be executed as long as condition is False.(i.e.,) The Loop should be repeated till the condition is True.
dim a(4)
dim i
Dim Data : data=" "
for i=o to 4 step 1
a(i)= int (inputbox("Enter Value (1 to 1000) @ a("& i &")"))
if(a(i)<1 or a(i)>1000) then
Exit For
end if
Next
data=" "
If(i<4) then
msgbox "For Loop got break"
end if
for each i in a ' For Each Loop
data= data & i &","
Next
msgbox "Data from ForEach Loop" & data
data=" "
' While....Wend Loop
While i<=4
tempd=" " & vbcrlf
for x=o to i
tempd = tempd & "*"
next
data = data & tempd
i=i+1
wend
msgbox "Data from While..Wend. Loop" & data
' Do...While Loop
data=" "
i=0
Do
tempd=" " & vbcrlf
for x=o to i
tempd = tempd & x+1
next
i=i+1
data = data & tempd
loop While i<=4
msgbox "Data from Do..While. Loop" & data
' Do...until Loop
data=" "
i=0
Do
tempd=" " & vbcrlf
for x=o to i
tempd = tempd & x+1
next
i=i+1
data = data & tempd
loop until i>=3
msgbox "Data from Do..until. Loop" & data