Solved

Loop skip

Posted on 2003-10-31
10
989 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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