Solved

Loop skip

Posted on 2003-10-31
10
990 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

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

740 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