Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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

like image 278
EKI Avatar asked Oct 15 '10 16:10

EKI


People also ask

How do you continue to next iteration in For Loop?

If you want to skip a particular iteration, use continue . If you want to break out of the immediate loop, use break.

How do you continue in VBA?

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.

Does VBScript have a CONTINUE 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:

How do I skip to the next iteration of a loop?

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.

How to short-circuit a for loop in VBScript?

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.

Where does the continue while statement go in a for loop?

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.


2 Answers

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.

like image 26
crush Avatar answered Sep 21 '22 05:09

crush


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 
like image 165
Tmdean Avatar answered Sep 21 '22 05:09

Tmdean