“继续” (到下一次迭代)在 VBScript 上
我和一位同事试图找出一种方法,在 VBScript“For/Next”循环中执行相当于“继续”语句的操作。
我们到处寻找,发现人们无法在 VBScript 中做到这一点而不需要令人讨厌的嵌套,这对我们来说不是一个选择,因为它是一个相当大的循环。
我们提出了这个想法。它会像“继续(到下一次迭代)”一样工作吗?有没有人有更好的解决方法或改进建议?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的建议可行,但使用 Do 循环可能更具可读性。
这实际上是 C 语言中的一个习惯用法 - 如果您想尽早摆脱构造,您可以使用 do { } while (0) 循环和break语句,而不是使用 goto 。
正如美眉建议的那样,如果去掉额外的缩进级别,看起来会好一些。
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.
As crush suggests, it looks a little better if you remove the extra indentation level.
我决定使用布尔变量来跟踪 for 循环是否应该处理其指令或跳到下一次迭代:
我发现嵌套循环解决方案在可读性方面有些令人困惑。此方法也有其自身的缺陷,因为循环在遇到
continue
后不会立即跳到下一次迭代。稍后的条件可能会反转继续
的状态。它在初始循环中还有一个辅助构造,并且需要声明一个额外的变量。哦,VBScript...叹息。
另外,如果您想使用已接受的答案(可读性还不错),您可以将其与使用
:
结合起来,将两个循环合并为一个循环:我发现它对于消除额外的缩进很有用。
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: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 ofcontinue
. 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:I found it useful to eliminate the extra level of indentation.
一种选择是将循环中的所有代码放入
Sub
内,然后当您想要“继续”时从该Sub
返回。并不完美,但我认为额外的循环不会那么令人困惑。
One option would be to put all the code in the loop inside a
Sub
and then just return from thatSub
when you want to "continue".Not perfect, but I think it would be less confusing that the extra loop.
我过去经常使用 Do、Loop,但我已经开始使用可以退出的 Sub 或 Function。对我来说它看起来更干净。如果您需要的任何变量不是全局的,您还需要将它们传递给 Sub。
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.
我们可以使用单独的函数来执行 continue 语句工作。假设您有以下问题:
这里我们将使用 for 循环体的函数调用:
循环将继续 for 函数退出语句......
We can use a separate function for performing a continue statement work. suppose you have following problem:
Here we will use a function call for for loop body:
loop will continue for function exit statement....
我一直使用 Do While 循环。这适用于两种 For 类型循环。
恕我直言,这看起来很干净。
我刚刚看到了 Crush 的回答,在底部。抱歉重复的答案。
I've always used an Do While loop. This works with both For type loops.
IMHO, this just looks clean.
I just saw Crush's answer, at the bottom. Sorry for the duplicate answer.
将迭代实现为递归函数。
首先调用 Iterate( 1, N )
Implement the iteration as a recursive function.
Start with a call to Iterate( 1, N )
尝试使用 While/Wend 和 Do While / Loop 语句...
Try use While/Wend and Do While / Loop statements...
我认为您打算在
if
语句下包含所有逻辑。基本上:这可能会使代码更长一些,但有些人不这样做无论如何,我不喜欢
break
或continue
。I think you are intended to contain ALL YOUR LOGIC under your
if
statement. Basically:This can make the code a bit longer, but some people don't like
break
orcontinue
anyway.