Forum Discussion

Lee_M's avatar
Lee_M
Community Hero
4 years ago

breaking out of loop iteration

I have created a Data Driven loop with an if condition

 

the condition will jump to the next iteration should the criteria NOT be met, how can I break out of the current cycle ?

 

The only operation I have found in keyword is "STOP EXECUTION" which will stop the script working which is not what I am looking for

 

e.g.

 

 

loop (i=1) {

 

  if (i == 3) {

    break; 

  }

 

  // do something

 

  i++;

}

  • Hi,

     

    You may consider Label and Go To Label operations.

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    You may consider Label and Go To Label operations.

     

    • Lee_M's avatar
      Lee_M
      Community Hero

       

      Thanks, I was unaware of this VB(ish) goto function

      This 50% does what I was loop for

       

      I have compiled a small test with a goto operation - this works as expected

       

      The issue I have now if that this loop will never end, the iteration restarts but the value is still the same

       

      I need to break out of the loop but move to the NEXT iteration

       

       

      e.g.

      loop (i=1) {

       

        if (i == 3) {

          i++;

          break; 

        }

       

        // do something

       

        i++;

      }

  • I don't think you need a break.  Try this:

     

     

    loop (i=1) {

     

      if (i != 3) {

      // do something

      }

     

      i++;

    }

    • Lee_M's avatar
      Lee_M
      Community Hero

       

      My Code was just to serve as an example

      I know that you can do negative conditions as that will do quicker and easier in most cases without having to process other steps that are not needed - saving time

       

      My question was not specific to this example and more "how do you do this ?"