“继续” (下一个迭代)在VBScript上
我和一个同事试图找出一种在vbscript中进行“继续”循环中“继续”语句的方法。
到处都是,我们发现人们在没有令人讨厌的巢穴的情况下无法在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)
一个选项是将所有代码放入
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,经常使用循环,但是我已经开始使用可以退出的子或功能。对我来说似乎更干净。如果您不需要的任何变量不是全局,则需要将它们传递给潜艇。
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.
我一直在循环时使用DO。这与两种类型循环一起使用。
恕我直言,这看起来很干净。
我刚刚在底部看到了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.
我们可以使用单独的功能来执行继续语句工作。假设您有以下问题:
在这里,我们将使用一个函数呼叫进行循环主体:
循环将继续进行函数退出语句。
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....
将迭代作为递归函数实施。
从呼叫迭代(1,n)开始
Implement the iteration as a recursive function.
Start with a call to Iterate( 1, N )
尝试使用 / wend and wend and while / loop语句...
Try use While/Wend and Do While / Loop statements...
我认为,如果语句,您打算在
下包含所有逻辑。基本上:
这可以使代码更长一些,但是有些人 don> don' t喜欢
break
或继续
无论如何。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.您的建议会起作用,但是使用DO循环可能会更可读性。
这实际上是C中的一个习语 - 而不是使用goto,而是可以有一个do {},而(0)循环带有断路语句,则如果您想及早救出构造。
正如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.
As crush suggests, it looks a little better if you remove the extra indentation level.
我决定涉及使用布尔变量的一种解决方案来跟踪循环的
是否应该处理其指令或跳过下一个迭代:
我发现嵌套的环解决方案在某种程度上使人感到困惑。此方法还具有自己的陷阱,因为循环遇到
继续
后,循环不会立即跳过下一次迭代。以后的条件可能会扭转继续
的状态。它还在初始循环中具有辅助构建体,需要声明额外的var。哦,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.