I still get the occasional Classic ASP VBScript maintenance job from time to time, and it reminds me of the language statements it just doesn’t have. Like “continue”, great for skipping a bunch of logic inside a For loop. Turns out, there is a nice simple way to get that functionality.
A big hat tip to Stack Overflow, once again. This tip comes directly from a question ‘“Continue” (to next iteration) on VBScript‘ which I found on the first quick search.
If I have a loop with some logic that I only want to run when some conditions are satisfied, it can get pretty messy with nested If statements. Or I can short-circuit the logic by just skipping to the next iteration of the loop, if I have a Continue statement. This is what it would look like:
For Each account In accounts If condition1(account) Then Continue If condition2(account) Then Continue If condition3(account) Then Continue ' do something with account... Next
Most modern languages support such a statement. VBScript not so much. Of course, the conditional tests could all be handled within a function, but that can sometimes make the whole logic less clear. The construct above is very clear and concise. 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:
For Each account In accounts Do ' null loop for logic short-circuit If condition1(account) Then Exit Do If condition2(account) Then Exit Do If condition3(account) Then Exit Do ' do something with account... Loop While False ' end of null loop Next
I just love it when the job can still be done in old languages like VBScript!