[vbscript] "Continue" (to next iteration) on VBScript

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

This question is related to vbscript iteration next continue

The answer is


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

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 )


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.


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.


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.


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

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


Examples related to vbscript

How to run VBScript from command line without Cscript/Wscript How to set recurring schedule for xlsm file using Windows Task Scheduler How to prevent 'query timeout expired'? (SQLNCLI11 error '80040e31') How do I rename a file using VBScript? How to get two or more commands together into a batch file How to run vbs as administrator from vbs? Find specific string in a text file with VBS script Getting current directory in VBScript Run Command Line & Command From VBS Permission denied on CopyFile in VBS

Examples related to iteration

Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply? How to loop over grouped Pandas dataframe? How to iterate through a list of dictionaries in Jinja template? How to iterate through an ArrayList of Objects of ArrayList of Objects? Ways to iterate over a list in Java Python list iterator behavior and next(iterator) How to loop through an array containing objects and access their properties recursion versus iteration What is the perfect counterpart in Python for "while not EOF" How to iterate over a JavaScript object?

Examples related to next

iterrows pandas get next rows value Passing variables to the next middleware using next() in Express.js How to read one single line of csv data in Python? Android Button click go to another xml page Continue For loop javascript node.js next() "Continue" (to next iteration) on VBScript jquery, find next element by class Getting next element while cycling through a list

Examples related to continue

Is there a difference between `continue` and `pass` in a for loop in python? Example use of "continue" statement in Python? Equivalent of "continue" in Ruby "Continue" (to next iteration) on VBScript Nested jQuery.each() - continue/break Using continue in a switch statement Difference between break and continue statement What is the "continue" keyword and how does it work in Java?