While Loop is an obsolete structure, I would recommend you to replace "While loop" to "Do While..loop", and you will able to use Exit clause.
We can exit any Do loop by using the Exit Do statement.
Breaking Out of While Loops. To break out of a while loop, you can use the endloop, continue, resume, or return statement.
Well an easy way to do this is to declare a global variable that gets set to false until an error occured, and if the error occured then that variable will be set to true, after that anytime that variable gets checked you can exit sub\function\for\do\whatever.
VBScript's While
loops don't support early exit. Use the Do
loop for that:
num = 0
do while (num < 10)
if (status = "Fail") then exit do
num = num + 1
loop
what about changing the while loop to a do while loop
and exit using
Exit Do
While Loop is an obsolete structure, I would recommend you to replace "While loop" to "Do While..loop", and you will able to use Exit clause.
check = 0
Do while not rs.EOF
if rs("reg_code") = rcode then
check = 1
Response.Write ("Found")
Exit do
else
rs.MoveNext
end if
Loop
if check = 0 then
Response.Write "Not Found"
end if}
Incredibly old question, but bearing in mind that the OP said he does not want to use Do While
and that none of the other solutions really work... Here's something that does exactly the same as a Exit Loop
:
This never runs anything if the status is already at "Fail"...
While (i < 20 And Not bShouldStop)
If (Status = "Fail") Then
bShouldStop = True
Else
i = i + 1
'
' Do Something
'
End If
Wend
Whereas this one always processes something first (and increment the loop variable) before deciding whether it should loop once more or not.
While (i < 20 And Not bShouldStop)
i = i + 1
'
' Do Something
'
If (Status = "Fail") Then
bShouldStop = True
End If
Wend
Ultimately, if the variable Status
is being modified inside the While
(and assuming you don't need i
outside the while, it makes no difference really, but just wanted to present multiple options...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With