Solved

Loop skip

Posted on 2003-10-31
10
988 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

15 Experts available now in Live!

Get 1:1 Help Now