Vbscript Exit For Loop To Break

Updated on 20 Feb 2012,
Published on 31 Jul 2008

Vbscript Exit For is used to break the iterative sequence of for loop counter. Generally Exit For keyword of vbscript is placed inside if then condition block that tests the value of counter variable or any other parameter evaluated using for loop counter variable. When vbscript if condition returns true then exit for statement gets executed just after the code lines placed inside if then condition block. Vbscript Exit For keyword terminates the for loop iterations before its complete execution.

Classic ASP VbScript Array Functions Examples:

You can see the live samples and examples of Classic ASP VbScript Array Functions from the following links:

Syntax of Vbscript Exit For Loop To Break

For variableName = 0 To 10

'code lines here

    If [condition] then

          'code lines here

        Exit For

    End If

Next

In the above syntax you can see that Exit For is coded inside the if then condition. When if condition returns true and passes the test then code block inside the if condition block gets executed and Exit For code statement breaks the repeating code execution sequence of for loop iterations.

Example of Vbscript Exit For Loop To Break

<%

Dim myArray(11)

myArray(0) = "January"

myArray(1) = "February"

myArray(2) = "March"

myArray(3) = "April"

myArray(4) = "May"

myArray(5) = "June"

myArray(6) = "July"

myArray(7) = "August"

myArray(8) = "September"

myArray(9) = "October"

myArray(10) = "November"

myArray(11) = "December"

Dim counter

For counter = 0 To UBound(myArray)

Response.Write(myArray(counter) & "<br />")

    If counter = 5 Then

        Exit For

    End If

Next

Response.Write("<br />")

Response.Write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />")

Response.Write("<br />")

For counter = UBound(myArray) To 0 step -1

Response.Write(myArray(counter) & "<br />")

    If counter = 6 Then

        Exit For

    End If

Next

%>

Output:

You can see the output of above discussed code from the following link:

VbScript Array Exit For Loop to Break

Continue to next tutorial: Vbscript Add to Array using For Loop to learn how increase the length of an Array and add items dynamically using for loop.

4 Responses to "Vbscript Exit For Loop To Break"
Saket
wel this is about for loop exit but how can we exit while loop?? i am having a code below in which if i find specific record i want to come out of while loop. i tried exit while and exit statements but thjey are not working. It is giving error that its a invalid statement


while not obj.EOF
if obj("user_id")=session("id") and obj("email")=Request("to") then
Dim con1,obj1

Set con1 = Server.createObject("ADODB.Connection")
Set obj1 = Server.createObject("ADODB.RecordSet")

con1.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Inetpub\wwwroot\Project\PENTA_DB.mdb"
con1.open

obj1.Open "SELECT * FROM inbox",con1,1,3
obj1.addNew
var="IN" & obj1("id")
obj1.Fields(1)=var
obj1.Fields(2)=session("id")
obj1.Fields(3) = Request("to")
session("to")=Request("to")
obj1.Fields(4) = Request("subject")
session("sub")=Request("subject")
obj1.Fields(5) = Request("cont")
session("cont")=Request("cont")
obj1.update
session("mailid")=var
session("m_id")=obj1.Fields(0)
temp=1

else
Dim co,ob,tem

Set co = Server.createObject("ADODB.Connection")
Set ob = Server.createObject("ADODB.RecordSet")

co.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Inetpub\wwwroot\Project\PENTA_DB.mdb"
co.open

ob.Open "SELECT * FROM spam",co,1,3
ob.addNew
tem="SP" & ob("id")
ob.Fields(1)=tem
ob.Fields(2)=session("id")
ob.Fields(3) = Request("to")
session("to")=Request("to")
ob.Fields(4) = Request("subject")
session("sub")=Request("subject")
ob.Fields(5) = Request("cont")
session("cont")=Request("cont")
ob.update
session("mailid")=tem
session("m_id")=ob.Fields(0)
temp=0

end if
obj.movenext
wend
Ezineasp.net
Hi Saket

Try to apply [b]Do...While loop[/b] here and use [b]Exit Do[/b] to exit the loop process.

E.g.:

obj.Open "SELECT * FROM inbox", con, 1, 3

IF obj.RecordCount > 0 Then

Do

if obj.Fields(0) = 1 then
exit do
end if

Response.Write obj.Fields(1) & "<br>"
obj.MoveNext()

Loop While Not obj.EOF

END IF

Hope this will work for you.
Ramkumar
msg="hi"
counter=0
while (true)
if msg="hi" then
msgbox counter & " hi"
counter=counter+1
else
msgbox "sfsdaf"
end if

if counter=10 then
HOW CAN I EXIT FROM HERE???????????????
end if
wend
Ezineasp.net
Hi Ramkumar You must use while (counter<10) to exit the while loop when counter reaches to value 10. Hope this will do.
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email