CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Do you know if VB has a similiar "continue" statement like in C?

    Do you know if VB has a similiar "continue" statement? I want the statement to exit the current loop(i) then go into the next loop(i+1).

    Thanks!

    Best Regards,

    Kevin Shen
    Best Regards,

    Kevin Shen

  2. #2
    Join Date
    May 2001
    Posts
    40

    Re: Do you know if VB has a similiar "continue" statement like in C?

    Yes it does

    Exit Do




  3. #3
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Do you know if VB has a similiar "continue" statement like in C?

    The only way that i know of is to set up a label, just before the end of the loop and when a certain condition is met, jump to that label:

    for i = 1 to 20
    if i mod 7 = 0 then
    Goto NextOne
    else
    DoSomethingElse
    end if

    NextOne:

    next i




    something like that should work.

    john

    John Pirkey
    MCSD
    http://www.ShallowWaterSystems.com
    http://www.stlvbug.org
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  4. #4
    Join Date
    May 2001
    Posts
    40

    Re: Do you know if VB has a similiar "continue" statement like in C?

    ooops sorry... that's what I get for reading too fast


  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Do you know if VB has a similiar "continue" statement like in C?

    ...but you could do the contrary:
    do something only if it matches your criteria,
    else... do nothing and go on counting!
    ie:

    for i = 1 to 20
    'if matched, do something
    if i mod 7 <> 0 then
    DoSomethingElse
    end if
    'if not matches, do nothing and just
    'add 1 to i
    next i





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Feb 2000
    Posts
    137

    Re: Do you know if VB has a similiar "continue" statement like in C?

    How about...



    for i = 1 to 20
    'Check for skip condition
    If i = 4 then
    i = i + 1
    else 'Do work
    Tmp = Tmp + i
    End If
    next i




    The loop will add i to Tmp until i = 4, this iteration is skipped, then the loop continues with 5 to add i to Tmp. Is this more what you meant?

    Hope this helps.
    Spectre



  7. #7
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Do you know if VB has a similiar "continue" statement like in C?

    It is a bad practice to change the loop index inside a loop. It can lead to errors that are difficult to debug. You could do with a Do...Loop statement instead, if the index needs to be changed.


  8. #8
    Join Date
    Feb 2000
    Posts
    137

    Re: Do you know if VB has a similiar "continue" statement like in C?

    I've been using this technique for almost 10 years and have yet for it to cause a problem - let alone an error that is hard to find, however, if you are uncomfortable with it don't do it.

    Hope this helps.
    Spectre



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

Featured