我和一位同事试图找出一种在 VBScriptFor/Next"循环中执行继续"语句的等效方法.
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.
我们到处都发现人们无法在 VBScript 中执行此操作而没有讨厌的嵌套,这对我们来说不是一个选项,因为它是一个非常大的循环.
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感谢您的评论
推荐答案您的建议可行,但使用 Do 循环可能更具可读性.
Your suggestion would work, but using a Do loop might be a little more readable.
这实际上是 C 中的一个习惯用法 - 如果您想尽早退出该构造,则可以使用带有 break 语句的 do { } while (0) 循环来代替使用 goto.
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正如crush所暗示的,如果你去掉额外的缩进级别,它看起来会更好一些.
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更多推荐
“继续"(到下一次迭代)在 VBScript 上
发布评论