Goto in For-loop

Discussion in 'Questions (Windows Mobile)' started by miklquick, Nov 18, 2008.

  1. miklquick

    miklquick Member Licensed User

    Dear professionals,

    I have a for-loop in which under certain conditions it should skip part of the code and jump to the next increment (Next i). e.g.:

    For i = 1 To 3
    If i= 2 Then Goto JUMP
    Msgbox(i)
    JUMP:
    Next

    A long as the if-condition (i = 2) is not met it works but if i=2 then it goes to JUMP but on reaching "Next" provides a runtime error "syntax error".

    Can anyone help me how this can be made. I also tried "if i=2 Then Next i" but this leads to an error upon compilation.

    Regards miklquick
     
  2. petrbury

    petrbury Member Licensed User

    As I'm not a "professional" I don't know why it doesn't work. But you can try this way

    For i = 1 To 3
    If i <> 2 Then Msgbox(i)
    Next

    Regards Petr
     
  3. giannimaione

    giannimaione Well-Known Member Licensed User

    or
    Code:
    For i=1 To 3
      
    If i<>2 Then
         
    Msgbox(i)
      
    End If
    Next i
     
  4. Cableguy

    Cableguy Expert Licensed User

    Try it this way...
    Code:
    For i = 1 To 3
    'do what ever HAS to be done always....
    If i= 2 Then
    'leving this blank will cause the loop to do nothing
    else
    'Do what ever is left to be don if the condition was not met
    End if
    Next
     
  5. miklquick

    miklquick Member Licensed User

    Thank you all for your help.

    it appears that the if-clause is the only solution.

    I know that "goto"s are not very good and should normally be avoided but in my case it would have made the code easier and shorter as the "MsgBox(i)" stood for a complex code and a further if-clause makes the code even more complex.

    Thanks anyway

    Miklquick
     
  6. agraham

    agraham Expert Licensed User

    I would have expected that to work as it is jumping within the same scope, and indeed it does work as expected if it is optimised compiled. I think, perhaps wrongly, that it would have worked in the past in the IDE or a legacy app. Maybe Erel has changed something.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You can use Exit.
    Code:
    For i = 1 To 3
       
    If i= 2 Then Exit
       
    Msgbox(i)
    Next
     
  8. agraham

    agraham Expert Licensed User

    I suspect he wanted the loop to continue and not to end. Has something changed in the last year to cause this to happen or has it always been there?
     
  9. miklquick

    miklquick Member Licensed User

    you are exactly right agraham, the For-loop should continue!

    miklquick
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Sorry for missing this important point.
    Jumping inside loops is technically not supported. It wasn't supported in previous versions as well.
    Goto labels can only be in the most upper scope.
     
  11. klaus

    klaus Expert Licensed User

    Hi miklquick,

    I don't really understand in what the code with the Goto
    Code:
    For i = 1 To 3
      
    If i= 2 Then Goto JUMP
        
    Msgbox(i)
    JUMP:
    Next
    is easier or shorter than giannimaione one's.
    Code:
    For i=1 To 3
      
    If i<>2 Then
         
    Msgbox(i)
      
    End If
    Next i
    The only difference is the statement i<>2 instead of i=2 and JUMP is replaced by End If.

    As the code replacing MsgBox(i) is elatively long you could also use this code to make it better readable:
    Code:
    For i=1 to 3
      
    If i<>2 Then
        DoSomething
      
    End If
    Next
     
     
    Sub DoSomething
      
    MsgBox(i)
      ...
      ...
    End Sub
    Anyway there are always several solutions.

    Best regards
     
    Last edited: Nov 19, 2008
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice