VB.NET - How to move to next item a For Each Loop?

vb.netLoops

vb.net Problem Overview


Is there a statment like Exit For, except instead of exiting the loop it just moves to the next item.

For example:

For Each I As Item In Items

    If I = x Then 
        ' Move to next item
    End If

    ' Do something

Next

I know could simply add an Else to the If statement so it would read as follows:

For Each I As Item In Items

    If I = x Then 
        ' Move to next item
    Else
        ' Do something
    End If

Next

Just wondering if there is a way to jump to the next item in the Items list. I'm sure most will properly be asking why not just use the Else statement, but to me wrapping the "Do Something" code seems to be less readable. Especially when there is a lot more code.

vb.net Solutions


Solution 1 - vb.net

For Each I As Item In Items
    If I = x Then Continue For

    ' Do something
Next

Solution 2 - vb.net

I'd use the Continue statement instead:

For Each I As Item In Items

    If I = x Then
        Continue For
    End If

    ' Do something

Next

Note that this is slightly different to moving the iterator itself on - anything before the If will be executed again. Usually this is what you want, but if not you'll have to use GetEnumerator() and then MoveNext()/Current explicitly rather than using a For Each loop.

Solution 3 - vb.net

What about:

If Not I = x Then

  ' Do something '

End If

' Move to next item '

Solution 4 - vb.net

I want to be clear that the following code is not good practice. You can use GOTO Label:

For Each I As Item In Items

    If I = x Then
       'Move to next item
        GOTO Label1
    End If

    ' Do something
    Label1:
Next

Solution 5 - vb.net

When I tried Continue For it Failed, I got a compiler error. While doing this, I discovered 'Resume':

For Each I As Item In Items

    If I = x Then
       'Move to next item
       Resume Next
    End If
    
    'Do something

Next

Note: I am using VBA here.

Solution 6 - vb.net

Only the "Continue For" is an acceptable standard (the rest leads to "spaghetti code").

At least with "continue for" the programmer knows the code goes directly to the top of the loop.

For purists though, something like this is best since it is pure "non-spaghetti" code.

Dim bKeepGoing as Boolean 
For Each I As Item In Items
  bKeepGoing = True
  If I = x Then
    bKeepGoing = False
  End If
  if bKeepGoing then
    ' Do something
  endif
Next

For ease of coding though, "Continue For" is OK. (Good idea to comment it though).

Using "Continue For"

For Each I As Item In Items
  If I = x Then
    Continue For   'skip back directly to top of loop
  End If
  ' Do something
Next

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSean TaylorView Question on Stackoverflow
Solution 1 - vb.netAdam RobinsonView Answer on Stackoverflow
Solution 2 - vb.netJon SkeetView Answer on Stackoverflow
Solution 3 - vb.nettimo2oo8View Answer on Stackoverflow
Solution 4 - vb.netSyed Tayyab AliView Answer on Stackoverflow
Solution 5 - vb.netKarmendraView Answer on Stackoverflow
Solution 6 - vb.netChris RaisinView Answer on Stackoverflow