"Continue" (to next iteration) on VBScript

EKI picture EKI · Oct 15, 2010 · Viewed 102.5k times · Source

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

Thanks for your comments

Answer

Tmdean picture Tmdean · Oct 15, 2010

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