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

发布于 2024-09-28 03:46:09 字数 481 浏览 5 评论 0原文

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

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

发布评论

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

评论(9

莫相离 2024-10-05 03:46:09

您的建议可行,但使用 Do 循环可能更具可读性。

这实际上是 C 语言中的一个习惯用法 - 如果您想尽早摆脱构造,您可以使用 do { } while (0) 循环和break语句,而不是使用 goto 。

Dim i

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

正如美眉建议的那样,如果去掉额外的缩进级别,看起来会好一些。

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
旧城烟雨 2024-10-05 03:46:09

我决定使用布尔变量来跟踪 for 循环是否应该处理其指令或跳到下一次迭代:

Dim continue

For Each item In collection
    continue = True

    If condition1 Then continue = False End If

    If continue Then
        'Do work
    End If
Next

我发现嵌套循环解决方案在可读性方面有些令人困惑。此方法也有其自身的缺陷,因为循环在遇到 continue 后不会立即跳到下一次迭代。稍后的条件可能会反转继续的状态。它在初始循环中还有一个辅助构造,并且需要声明一个额外的变量。

哦,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.

维持三分热 2024-10-05 03:46:09

一种选择是将循环中的所有代码放入 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.

装迷糊 2024-10-05 03:46:09

我过去经常使用 Do、Loop,但我已经开始使用可以退出的 Sub 或 Function。对我来说它看起来更干净。如果您需要的任何变量不是全局的,您还需要将它们传递给 Sub。

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
雨落星ぅ辰 2024-10-05 03:46:09

我们可以使用单独的函数来执行 continue 语句工作。假设您有以下问题:

for i=1 to 10

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

Next

这里我们将使用 for 循环体的函数调用:

for i=1 to 10
Call loopbody()
next

function loopbody()

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

End Function

循环将继续 for 函数退出语句......

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....

瞎闹 2024-10-05 03:46:09

我一直使用 Do While 循环。这适用于两种 For 类型循环。

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.

未央 2024-10-05 03:46:09

将迭代实现为递归函数。

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

首先调用 Iterate( 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 )

终陌 2024-10-05 03:46:09

尝试使用 While/Wend 和 Do 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
牵强ㄟ 2024-10-05 03:46:09

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

' 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

这可能会使代码更长一些,但有些人不这样做无论如何,我不喜欢breakcontinue

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.

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