Solved

Loop skip

Posted on 2003-10-31
10
987 Views
Last Modified: 2008-02-01
I'm using vb .NET and looking for the command in a for next loop that doesn't exit the loop but skips to the next iteration of it....
0
Comment
Question by:thenrich
  • 4
  • 2
  • 2
  • +2
10 Comments
 

Expert Comment

by:HAMMER33333
ID: 9659250
The only thing I can think of would be to use a Try/Catch whereby you can get out of it to proceed to the next iteration:

Do
       Try
              If condition NOT met Then
                  ' Go to next iteration
                   Exit Try
              End If
       Catch ex as exception
              MsgBox("Error: " & ex.message)
       End Try
Loop
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9659446
You have two basic options:

For xxxx
   If condition met then
      <execute code>
   End if
Next

-- or --

For x = 1 to 10
   <execute code>
   If somecondition Then
      x = x + 1
   End if
Next


Maybe if you can be a little more specific I can give better details.  First example simply wraps all code inside the loop so you can choose to execute it or "skip" it based on some condition.  Second example allows you after executing code in a loop to skip the next itteration - but you have to be very very careful anytime you change the counter within the loop.
0
 
LVL 4

Expert Comment

by:krznpsk
ID: 9659662
RESUME NEXT is what you're looking for, I think.

    For i As Integer = 0 To 100
            If condition = False Then
                Resume Next
            End If
        Next
0
 
LVL 5

Author Comment

by:thenrich
ID: 9659715
krznpsk you have the right idea but the RESUME NEXT doesn't work if I have the For Next loop wrapped in a try block - which I do..

frodoman and HAMMER33333,
I need something similiar to krznpsk's idea because I have multiple criteria at several points in my loop where I may just want to go to the next iteration.

I know in other laguages I think there is a command called 'continue' that does this....
0
 
LVL 4

Expert Comment

by:daveky
ID: 9659865
You need to move the try inside the for..next

for x = 1 to 10
  try
    'do some work
  catch ex as exception
    resume next
  end try
next

If you do it the other way round when an exception occurs you have lost the loop.

try
  for x = 1 to 10
    'do some work
  next
catch ex as exception
  'oops! you are out of the loop!
end try

Doing the try inside the loop should not cause you a difficulties as any problems you can recover from you should be checking for, rather than waiting for an exception, as exceptions are very inneficient.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:krznpsk
ID: 9659948
Okay - since a "Resume" won't work, then you can still use a GoTo.  This should solve your problem:

        Try
            For i As Integer = 1 To 100
beginFor:
                If condition Then
                    i = i + 1
                    GoTo beginFor
                End If
            Next
        Catch ex As Exception
        End Try
0
 
LVL 4

Expert Comment

by:krznpsk
ID: 9659978
Alternatively, you can use a GoTo to go to the line right above the "Next."  This way will also work in a For Each...Next loop.

        Dim ia() As Integer = {999, 444, 333, 222, 777}
        Try
            For Each i As Integer In ia
                If condition Then
                    GoTo endfor
                End If
endFor:
            Next
        Catch ex As Exception
        End Try
0
 
LVL 4

Expert Comment

by:krznpsk
ID: 9660156
OR...
simply nest the FOR in another procedure.

Sub trythis()
        Try
            nestedfor()
        Catch ex As Exception

        End Try

    End Sub
    Sub nestedfor()
        For i As Integer = 0 To 100
            If condition Then
                Resume Next
            End If
        Next
    End Sub
0
 
LVL 42

Accepted Solution

by:
frodoman earned 50 total points
ID: 9660224
thenrich,

You mentioned the "continue" command.  I'm sorry to say there's no such monster in vb.net - you're going to have to code in the logic yourself.

Several good possibilities, but given you have several conditions that you might throw you to the next itteration I'd just keep it simple.

For...
   bProcess = (condition1 met)
   If bProcess then
      < do something >
   End if

   If bProcess then bProcess = (condition2 met)
   If bProcess then
      < do something >
   End if

   If bProcess then bProcess = (condition3 met)
   If bProcess then
      < do something >
   End if

   etc.
   
Next

It's not elegant but it's clean & simple and works.  Good luck
0
 

Expert Comment

by:HAMMER33333
ID: 9660414
I've done something like this before:

Dim intSub1, intSub2 As Integer

Try
     For intSub1 = 0 to 100
            For intSub2 = 1 To 4       ' Four conditions to test
               Select Case intSub
                     Case 1
                           If <condition1> Then
                                 <do the work>
                                 Exit For
                           End If
                     Case 2
                           If <condition2> Then
                                  <do the work>
                                   Exit For
                           End If
                      Case 3
                           If <condition3> Then
                                  <do the work>
                                  Exit For
                            End If
                       Case 4
                            If <condition4> Then
                                 <do the work>
                                 Exit For
                            End If
               End Select
            Next
     Next
Catch ex As Exception
     MsgBox("Error: " & ex.message)
End Try



Hammer
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now