34

Is there a method of exiting/breaking a while in VBS/VBA?

Following code won't work as intended:

num = 0
while (num < 10)

    if (status = "Fail") then
        exit while
    end if

    num = num+1
wend
Selfish
  • 6,023
  • 4
  • 44
  • 63

7 Answers7

65

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
Helen
  • 87,344
  • 17
  • 243
  • 314
11

what about changing the while loop to a do while loop

and exit using

Exit Do
rahul
  • 184,426
  • 49
  • 232
  • 263
  • changing my while loop to do while loop will make my code even more complex.So i just found a workaround in which if u want to exit from a while loop using vbscripts,whenever the condition is set just set the while loop to the non matching condition of the loop.For eg: while(num<10) if(status = "Fail") then num = 10'here u just set the condition so that while lop fails end if –  Aug 14 '09 at 13:53
2

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}
JunzCode
  • 21
  • 1
  • 3
1

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...

cogumel0
  • 2,430
  • 5
  • 29
  • 45
  • In both of your examples the current iteration of the loop will continue to the end when it is set to "Fail". This does not act the same as an Exit statement where execution of the loop stops immediately and control is passed back to the outer logic block. – JoshHetland Dec 12 '16 at 15:41
  • So you're saying they're not the same because after `bShouldStop` is set to `True`, it jumps to `End If`, then goes back to evaluate the condition and finally goes to `Wend`, whereas an `Exit Loop` would just go straight to the `Loop` at the bottom. Sure, I'll give you that. But now let me ask you this: is this a **script** or an assembly application where each jump matters? The end result is **exactly** the same, no variables get changed, no code that shouldn't get executed gets executed, it stops when Status = Fail. But please, come up with a better solution using a while/wend... – cogumel0 Dec 13 '16 at 10:16
0

I know this is old as dirt but it ranked pretty high in google.

The problem with the solution maddy implemented (in response to rahul) to maintain the use of a While...Wend loop has some drawbacks

In the example given

num = 0
While num < 10
  If status = "Fail" Then
    num = 10
  End If
  num = num + 1
Wend

After status = "Fail" num will actually equal 11. The loop didn't end on the fail condition, it ends on the next test. All of the code after the check still processed and your counter is not what you might have expected it to be.

Now depending on what you are all doing in your loop it may not matter, but then again if your code looked something more like:

num = 0
While num < 10
  If folder = "System32" Then
    num = 10
  End If
  RecursiveDeleteFunction folder
  num = num + 1
Wend

Using Do While or Do Until allows you to stop execution of the loop using Exit Do instead of using trickery with your loop condition to maintain the While ... Wend syntax. I would recommend using that instead.

JoshHetland
  • 1,273
  • 1
  • 12
  • 22
  • *What* extra 3 characters? – cogumel0 Dec 09 '16 at 10:25
  • Oh man, that was 5 years ago. I think someone must have edited something. There was a comment at some point about saving characters with while wend over do while I think – JoshHetland Dec 10 '16 at 21:39
  • But bearing in mind the purpose of these questions is they're kept for posterity, including the answers, you should now edit yours to ensure it makes sense. – cogumel0 Dec 12 '16 at 15:19
0

While many have mentioned Do While...Loop, there are no examples showing the While is not necessary:

count = 0
Do
    count = count + 1
    wscript.echo count
    
    If (count > 9) Then
        Exit Do
    End If
    
    '...Some other logic that can be ended early...

    wscript.sleep(1000)
Loop

If you already have the variable to compare against and it makes sense to complete the loop, Do While... is great. However, if you're doing something a little more granular, like the WScript.Sleep above that delays the script can be avoided.

Sum None
  • 2,164
  • 3
  • 27
  • 32
-1

Use Do...Loop with Until keyword

num=0
Do Until //certain_condition_to_break_loop
 num=num+1
Loop

This loop will continue to execute, Until the condition becomes true

While...Wend is the old syntax and does not provide feature to break loop! Prefer do while loops