Forum Discussion

Sreera's avatar
Sreera
Occasional Contributor
6 years ago
Solved

Loop statements

How do I get my code to re-enter for loop after executing Exit For statement. Below is sample code

 

For i = 1 to x

 if instr(x,"abc") Then

    Exit For

Else

{statement}

End if

Next

{stmnt}

 

In the above case if I want my code to enter For loop again after executing {stmnt}, how do I proceed?

  • The whole point of the Exit statement for a for loop is to indicate that you have reached some sort of condition in the loop that you no longer need to continue looping.  This is true for any language, not just VB.

     

    So, if there is a special condition within the loop that you want to execute special code before you continue, what I would do is, rather than exit the loop, make "stmnt" a separate function and call that.  This way you can call stmnt within the loop and then, when stmnt is done, complete the loop.

4 Replies

  • m_essaid's avatar
    m_essaid
    Valued Contributor

    perhaps : ?

     

    For i = 1 to x

     if instr(x,"abc") Then

        Exit For

    Else

    {statement}

    End if

    {stmnt}

    Next

  • shankar_r's avatar
    shankar_r
    Community Hero

    Is there any purpose you want to go into Forr loop again. 

    Why are you exiting for loop? 

    What exactly you want to achieve from the above snippet?

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The whole point of the Exit statement for a for loop is to indicate that you have reached some sort of condition in the loop that you no longer need to continue looping.  This is true for any language, not just VB.

     

    So, if there is a special condition within the loop that you want to execute special code before you continue, what I would do is, rather than exit the loop, make "stmnt" a separate function and call that.  This way you can call stmnt within the loop and then, when stmnt is done, complete the loop.