“继续” (下一个迭代)在VBScript上

发布于 2025-01-27 21:29:53 字数 465 浏览 4 评论 0原文

我和一个同事试图找出一种在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

离旧人 2025-02-03 21:29:54

一个选项是将所有代码放入sub内的循环中,然后在要“继续”时从该sub返回。

并不完美,但我认为与额外的循环相比,这会减少混乱。

One option would be to put all the code in the loop inside a Sub and then just return from that Sub when you want to "continue".

Not perfect, but I think it would be less confusing that the extra loop.

惜醉颜 2025-02-03 21:29:54

我经常使用DO,经常使用循环,但是我已经开始使用可以退出的子或功能。对我来说似乎更干净。如果您不需要的任何变量不是全局,则需要将它们传递给潜艇。

For i=1 to N
 DoWork i
Next

Sub DoWork(i)
    [Code]
    If Condition1 Then
      Exit Sub
    End If

    [MoreCode]
    If Condition2 Then
      Exit Sub
    End If

    [MoreCode]
    If Condition2 Then
      Exit Sub
    End If

    [...]
End 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.

For i=1 to N
 DoWork i
Next

Sub DoWork(i)
    [Code]
    If Condition1 Then
      Exit Sub
    End If

    [MoreCode]
    If Condition2 Then
      Exit Sub
    End If

    [MoreCode]
    If Condition2 Then
      Exit Sub
    End If

    [...]
End Sub
怎言笑 2025-02-03 21:29:54

我一直在循环时使用DO。这与两种类型循环一起使用。

For iIndex = 0 to 100
  Do
    If bCondition then Exit Do
    ...
  Loop While False
Next 

恕我直言,这看起来很干净。
我刚刚在底部看到了Crush的答案。对不起,重复的答案。

I've always used an Do While loop. This works with both For type loops.

For iIndex = 0 to 100
  Do
    If bCondition then Exit Do
    ...
  Loop While False
Next 

IMHO, this just looks clean.
I just saw Crush's answer, at the bottom. Sorry for the duplicate answer.

半枫 2025-02-03 21:29:54

我们可以使用单独的功能来执行继续语句工作。假设您有以下问题:

for i=1 to 10

if(condition) then   'for loop body'
contionue
End If

Next

在这里,我们将使用一个函数呼叫进行循环主体:

for i=1 to 10
Call loopbody()
next

function loopbody()

if(condition) then   'for loop body'
Exit Function
End If

End Function

循环将继续进行函数退出语句。

We can use a separate function for performing a continue statement work. suppose you have following problem:

for i=1 to 10

if(condition) then   'for loop body'
contionue
End If

Next

Here we will use a function call for for loop body:

for i=1 to 10
Call loopbody()
next

function loopbody()

if(condition) then   'for loop body'
Exit Function
End If

End Function

loop will continue for function exit statement....

逆光下的微笑 2025-02-03 21:29:54

将迭代作为递归函数实施。

Function Iterate( i , N )
  If i == N Then
      Exit Function
  End If
  [Code]
  If Condition1 Then
     Call Iterate( i+1, N );
     Exit Function
  End If

  [Code]
  If Condition2 Then
     Call Iterate( i+1, N );
     Exit Function
  End If
  Call Iterate( i+1, N );
End Function

从呼叫迭代(1,n)开始

Implement the iteration as a recursive function.

Function Iterate( i , N )
  If i == N Then
      Exit Function
  End If
  [Code]
  If Condition1 Then
     Call Iterate( i+1, N );
     Exit Function
  End If

  [Code]
  If Condition2 Then
     Call Iterate( i+1, N );
     Exit Function
  End If
  Call Iterate( i+1, N );
End Function

Start with a call to Iterate( 1, N )

爱的那么颓废 2025-02-03 21:29:54

尝试使用 / wend and wend and while / loop语句...

i = 1
While i < N + 1
Do While true
    [Code]
    If Condition1 Then
       Exit Do
    End If

    [MoreCode]
    If Condition2 Then
       Exit Do
    End If

    [...]

    Exit Do
Loop
Wend

Try use While/Wend and Do While / Loop statements...

i = 1
While i < N + 1
Do While true
    [Code]
    If Condition1 Then
       Exit Do
    End If

    [MoreCode]
    If Condition2 Then
       Exit Do
    End If

    [...]

    Exit Do
Loop
Wend
ㄖ落Θ余辉 2025-02-03 21:29:54

我认为,如果语句,您打算在下包含所有逻辑。基本上:

' PRINTS EVERYTHING EXCEPT 4
For i = 0 To 10
  ' you want to say
  ' If i = 4 CONTINUE but VBScript has no continue
  If i <> 4 Then ' just invert the logic
    WSH.Echo( i )
  End If
Next

这可以使代码更长一些,但是有些人 don> don' t喜欢break继续 无论如何。

I think you are intended to contain ALL YOUR LOGIC under your if statement. Basically:

' PRINTS EVERYTHING EXCEPT 4
For i = 0 To 10
  ' you want to say
  ' If i = 4 CONTINUE but VBScript has no continue
  If i <> 4 Then ' just invert the logic
    WSH.Echo( i )
  End If
Next

This can make the code a bit longer, but some people don't like break or continue anyway.

紫轩蝶泪 2025-02-03 21:29:53

您的建议会起作用,但是使用DO循环可能会更可读性。

这实际上是C中的一个习语 - 而不是使用goto,而是可以有一个do {},而(0)循环带有断路语句,则如果您想及早救出构造。

Dim i

For i = 0 To 10
    Do
        If i = 4 Then Exit Do
        WScript.Echo i
    Loop While False
Next

正如Crush所建议的那样,如果您删除额外的缩进水平,看起来好多了。

Dim i

For i = 0 To 10: Do
    If i = 4 Then Exit Do
    WScript.Echo i
Loop While False: Next

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
冷情 2025-02-03 21:29:53

我决定涉及使用布尔变量的一种解决方案来跟踪循环的是否应该处理其指令或跳过下一个迭代:

Dim continue

For Each item In collection
    continue = True

    If condition1 Then continue = False End If

    If continue Then
        'Do work
    End If
Next

我发现嵌套的环解决方案在某种程度上使人感到困惑。此方法还具有自己的陷阱,因为循环遇到继续后,循环不会立即跳过下一次迭代。以后的条件可能会扭转继续的状态。它还在初始循环中具有辅助构建体,需要声明额外的var。

哦,vbscript ...叹息。

另外,如果您想使用所接受的答案,这还不错,明智的可读性,可以将其与合并到似乎是一个的合并到一个:

Dim i

For i = 0 To 10 : Do
    If i = 4 Then Exit Do
    WScript.Echo i
Loop While False : Next

我发现消除额外水平的凹痕很有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文