“继续"(到下一次迭代)在 VBScript 上 [英] "Continue" (to next iteration) on VBScript

查看:25
本文介绍了“继续"(到下一次迭代)在 VBScript 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一位同事试图找出一种在 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 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆