50

A colleague and I were trying to figure out a way of doing the equivalent of a "continue" statement within a VBScript "For/Next" loop.

Everywhere we looked we found people had no way to do this in VBScript without having nasty nestings, which is not an option for us since it is a quite big loop.

We came out with this idea. Would it work just as a "continue(to next iteration)"? Does anyone have any better workaround or improvement suggestion?

For i=1 to N
  For workaroundloop = 1 to 1
    [Code]
    If Condition1 Then
      Exit For
    End If
  
    [MoreCode]
    If Condition2 Then
      Exit For
    End If
  
    [MoreCode]
    If Condition2 Then
      Exit For
    End If
  
    [...]

  Next
Next
0

9 Answers 9

48

Your suggestion would work, but using a Do loop might be a little more readable.

This is actually an idiom in C - instead of using a goto, you can have a do { } while (0) loop with a break statement if you want to bail out of the construct early.

Dim i

For i = 0 To 10
    Do
        If i = 4 Then Exit Do
        WScript.Echo i
    Loop While False
Next

As crush suggests, it looks a little better if you remove the extra indentation level.

Dim i

For i = 0 To 10: Do
    If i = 4 Then Exit Do
    WScript.Echo i
Loop While False: Next
2
  • 6
    This isn't too bad. You could also use : to put the Do on the same line as For and the Loop While False on the same line as Next if you don't want the extra indentation level.
    – crush
    Jun 28, 2013 at 15:19
  • 2
    I don't think using : to put the Do on the same like is better. I looks cleaner at first glance but it reads very confusing. I think the first version (above) is much clearer and cleaner.
    – nyholku
    Apr 4, 2018 at 8:54
9

A solution I decided on involved the use of a boolean variable to track if the for loop should process its instructions or skip to the next iteration:

Dim continue

For Each item In collection
    continue = True

    If condition1 Then continue = False End If

    If continue Then
        'Do work
    End If
Next

I found the nested loop solutions to be somewhat confusing readability wise. This method also has its own pitfalls since the loop doesn't immediately skip to the next iteration after encountering continue. It would be possible for a later condition to reverse the state of continue. It also has a secondary construct within the initial loop, and requires the declaration of an extra var.

Oh, VBScript...sigh.

Also, if you want to use the accepted answer, which isn't too bad readability wise, you could couple that with the use of : to merge the two loops into what appears to be one:

Dim i

For i = 0 To 10 : Do
    If i = 4 Then Exit Do
    WScript.Echo i
Loop While False : Next

I found it useful to eliminate the extra level of indentation.

8

One option would be to put all the code in the loop inside a Sub and then just return from that Sub when you want to "continue".

Not perfect, but I think it would be less confusing that the extra loop.

3
  • 3
    If any, I would use the GoTo to go to the end of the loop so the counter goes +1, but I've come to understand that there is no GoTo support on VBScript :P
    – EKI
    Oct 16, 2010 at 23:42
  • VBSCRIPT HAS NO GOTO. That's really the whole point of the OP seeking continue. I did not mark you down. Suggest you edit it. :-) Jun 5, 2020 at 12:51
  • @www-0av-Com Well, I did say "I think", though not that I can remember what I thought 10 years ago :) Jun 5, 2020 at 19:18
5

I use to use the Do, Loop a lot but I have started using a Sub or a Function that I could exit out of instead. It just seemed cleaner to me. If any variables you need are not global you will need to pass them to the Sub also.

For i=1 to N
 DoWork i
Next

Sub DoWork(i)
    [Code]
    If Condition1 Then
      Exit Sub
    End If

    [MoreCode]
    If Condition2 Then
      Exit Sub
    End If

    [MoreCode]
    If Condition2 Then
      Exit Sub
    End If

    [...]
End Sub
1

We can use a separate function for performing a continue statement work. suppose you have following problem:

for i=1 to 10

if(condition) then   'for loop body'
contionue
End If

Next

Here we will use a function call for for loop body:

for i=1 to 10
Call loopbody()
next

function loopbody()

if(condition) then   'for loop body'
Exit Function
End If

End Function

loop will continue for function exit statement....

1
  • The 'exit sub' technique was already mentioned twice; adding typos and a misused function (no return value) doesn't help any. Jan 10, 2014 at 9:09
1

I've always used an Do While loop. This works with both For type loops.

For iIndex = 0 to 100
  Do
    If bCondition then Exit Do
    ...
  Loop While False
Next 

IMHO, this just looks clean. I just saw Crush's answer, at the bottom. Sorry for the duplicate answer.

3
  • I've been using this a LOT since I came across a reference to this. Many times I've had to check a lot of conditions, but didn't want to keep having to IF/ELSE IF to get through, because if something fails, I just want to get out of that code section (data validation for example). The DO/LOOP WHILE has been fantastic for this in VBSCRIPT. Just start a DO and end with a LOOP WHILE FALSE (this will cause that section to only execute once), then check all of your scenarios one by one as if they were all stand alone If any scenario fails, set any error values and then EXIT DO to leave the block. Jul 25, 2022 at 17:40
  • I've started adding a function (when applicable) for validating my data. This works best when there are a lot of conditions to test before entering the actual code. Doing it like that gives the line IF ValidateData( {any prams/array of prams / etc} ) then Exit Do Jul 26, 2022 at 20:34
  • Fred - looks like your validate function could also benefit from it's own DO/LOOP :P Jul 27, 2022 at 21:55
0

Implement the iteration as a recursive function.

Function Iterate( i , N )
  If i == N Then
      Exit Function
  End If
  [Code]
  If Condition1 Then
     Call Iterate( i+1, N );
     Exit Function
  End If

  [Code]
  If Condition2 Then
     Call Iterate( i+1, N );
     Exit Function
  End If
  Call Iterate( i+1, N );
End Function

Start with a call to Iterate( 1, N )

1
0

Try use While/Wend and Do While / Loop statements...

i = 1
While i < N + 1
Do While true
    [Code]
    If Condition1 Then
       Exit Do
    End If

    [MoreCode]
    If Condition2 Then
       Exit Do
    End If

    [...]

    Exit Do
Loop
Wend
1
  • Do... Loop While false is cleaner than Do While true... Exit Do... Loop
    – Stephen R
    Jun 17, 2019 at 17:36
0

I think you are intended to contain ALL YOUR LOGIC under your if statement. Basically:

' PRINTS EVERYTHING EXCEPT 4
For i = 0 To 10
  ' you want to say
  ' If i = 4 CONTINUE but VBScript has no continue
  If i <> 4 Then ' just invert the logic
    WSH.Echo( i )
  End If
Next

This can make the code a bit longer, but some people don't like break or continue anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.