Site Tools


Skipping current iteration of a VBScript For Loop

Developer: RhinoScript
Summary: Demonstrates how to skip the current iteration in a VBScript For loop.

Question

Both C++ and C# have a continue statement that, when used with a For loop, skips the remaining statements of that iteration and moves on to next iteration. Does VBScript have anything like this?

Answer

There is no continue or continue-like statement in VBScript. But using a Do While loop inside of a For Each statement, you can achieve the same functionality. For example:

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

Here is another example.

Sub TestContinue
 
  Dim arrTests, arrTest
 
  arrTests = Array( _
        Array(1) _
      , Array(1,2,3 ) _
      , Array(1,2) _
      , Array(1) _
      , Array(1,2,3) _
      )
 
  For Each arrTest In arrTests
     Call Rhino.Print("Process: {" & Join(arrTest, ", ") & "}")
     Do While True ' Continue trick
       Call Rhino.Print(" Process: " & arrTest(0))
       If 0 = UBound(arrTest) Then Exit Do ' Continue
       Call Rhino.Print(" Process: " & arrTest(1))
       If 1 = UBound(arrTest) Then Exit Do ' Continue
       Call Rhino.Print(" Process: " & arrTest(2))
       Exit Do
     Loop
  Next
 
End Sub
developer/scriptsamples/vbcontinue.txt ยท Last modified: 2015/09/14 (external edit)