"Continue" (to next iteration) on VBScript
Asked Answered
S

9

52

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
Swart answered 15/10, 2010 at 16:13 Comment(0)
L
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
Longford answered 15/10, 2010 at 17:33 Comment(2)
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.Luncheonette
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.Mvd
L
10

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.

Luncheonette answered 28/6, 2013 at 15:14 Comment(0)
O
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.

Orthotropous answered 15/10, 2010 at 16:22 Comment(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 :PSwart
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. :-)Aftersensation
@Aftersensation Well, I did say "I think", though not that I can remember what I thought 10 years ago :)Orthotropous
R
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
Restore answered 20/10, 2010 at 19:43 Comment(0)
R
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....

Rabkin answered 10/1, 2014 at 8:55 Comment(1)
The 'exit sub' technique was already mentioned twice; adding typos and a misused function (no return value) doesn't help any.Langford
G
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.

Gigantean answered 31/3, 2021 at 12:49 Comment(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.Longlegged
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 DoGigantean
Fred - looks like your validate function could also benefit from it's own DO/LOOP :PLonglegged
G
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 )

Garvey answered 15/10, 2010 at 16:29 Comment(1)
Though you have to be a little careful, if N is big enough, this could lead to a Stackoverflow (as mentioned in the spec here: msdn.microsoft.com/en-us/library/x7hbf8fa%28VS.85%29.aspx).Orthotropous
I
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
Invention answered 5/2, 2015 at 13:55 Comment(1)
Do... Loop While false is cleaner than Do While true... Exit Do... LoopCalla
B
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.

Birthroot answered 5/9, 2019 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.