Solved

Loop skip

Posted on 2003-10-31
10
991 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 134
asp.net figure out error 3 47
C# XML Get Values 4 53
Disable the weekends on datepicker control 6 51
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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