Results 1 to 8 of 8

Thread: Continue with next iteration

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Continue with next iteration

    In C or C++ you can exit the body of a loop and just move on to the next iteration without finishing, using the "continue" statement...

    Is there a way to do this in VB.Net?

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    VB Code:
    1. public sub Something()
    2.  
    3. if x <> something then
    4.  
    5. exit sub
    6.  
    7. else
    8.  
    9. do this
    10.  
    11. end if
    12.  
    13. end sub

    do you mean like this?

    [/Highlight]
    Last edited by Andy; Apr 1st, 2004 at 03:13 PM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Well, It could be done like that, but the idea is that it would not have to run through the rest of the lines of code, thus making the whole thing faster...

    I guess I'm stuck w/ the long way.

    Thanks for ur input,

    squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195
    Are you using a "For Each Next" or a "For Next" loop,

    If so just use "Exit For", this will get you out of the loop and just continue on with the rest of the code in the sub.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    The thing is... I don't want to get out of the loop. I want to stay in the loop, but not have to finish the rest of the code that is defined within the loop. Like this...

    VB Code:
    1. Dim i as int32
    2. For i = 0 to 9
    3.    i = i + 1
    4.    if i = 5 then
    5.       '<<whatever the code would be>>
    6.    end if
    7.    i = i - 1
    8. Next i

    With the ability to do what I'm thinking of, this code would eventually stop at the <<whatever the code would be>> part and move on to the next itteration where i would = 6 and it would just continue the loop as normal...

    Understand?

    squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  6. #6
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195
    Code:
    Dim i as int32
    For i = 0 to 9
       i = i + 1
       if i = 5 then
          '<<whatever the code would be>>
       end if
       i = i - 1
    Next i
    First off if I'm seeing this right, it's an infinite loop! i starts off 0 then is increased by 1, but then later decraesed by 1???

    The value i would never get higher than 1.

    Just let the For statement increase i.

    if you want to exit a loop a specific value then just use:



    Code:
    Dim i as int32
    For i = 0 to 9
         if i = 5 then
          '<<whatever the code would be>>
       i='Any Value you want to jump to
    end if
    Next i
    To be honest I still don't understand.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    You caught me... I wasn't paying too much attention to that code. But the idea is still the same. I just don't want to have to move through the rest of the code before going on to the next itteration of the loop. For instance...

    If I have some 30 lines of code within my for loop and after the first 5 i do some sort of check and realize that I don't need to go through the other 25 lines of code for that itteration, is there any way to get of of doing those other 25 lines?

    Just setting i = to whatever won't work b/c I'm still required to move through the rest of the lines before getting to the "next i" statement. I could use a goto statement and put a label just above the "next i" statement, but that just looks sloppy.

    Thanks,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  8. #8
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195
    As far as I can see the "Goto" statement is the cleanest way to go.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width