“Continue” (to next iteration) on VBScript

后端 未结 8 759
陌清茗
陌清茗 2021-02-03 17:43

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

相关标签:
8条回答
  • 2021-02-03 18:01

    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.

    0 讨论(0)
  • 2021-02-03 18:02

    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.

    0 讨论(0)
  • 2021-02-03 18:06

    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 )

    0 讨论(0)
  • 2021-02-03 18:09

    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.

    0 讨论(0)
  • 2021-02-03 18:11

    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
    
    0 讨论(0)
  • 2021-02-03 18:23

    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
    
    0 讨论(0)
提交回复
热议问题