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
If you want to skip a particular iteration, use continue . If you want to break out of the immediate loop, use break.
Continue and Exit For LoopsA Continue statement in loops is a statement that allows you to skip all remaining statements in your current loop iteration and proceed to the next loop iteration. Compared to Visual Basic, however, VBA (Visual Basic for Applications) does not have an equivalent of a Continue For statement.
If only VBScript had a Continue statement… But as Tmdean helpfully points out in his answer on Stack Overflow, you can easily simulate a Continue statement with a null Do…Loop and Exit Do! Here’s what the above becomes:
If you have nested loops of different types, for example a Do loop within a For loop, you can skip to the next iteration of either loop by using either Continue Do or Continue For. The following code example uses the Continue While statement to skip to the next column of an array if a divisor is zero. The Continue While is inside a For loop.
VBScript doesn't have a Continue statement for short-circuiting For loops, but you can achieve the same thing with a null Do Loop and Exit Do. VBScript doesn't have a Continue statement for short-circuiting For loops, but you can achieve the same thing with a null Do Loop and Exit Do.
The Continue While is inside a For loop. It transfers to the While col < lastcol statement, which is the next iteration of the innermost While loop that contains the For loop.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With