cancel
Showing results for 
Search instead for 
Did you mean: 

breaking out of loop iteration

SOLVED
Lee_M
Contributor

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++;

}

1 ACCEPTED SOLUTION

Accepted Solutions
AlexKaras
Community Hero

Re: breaking out of loop iteration

Hi,

 

You may consider Label and Go To Label operations.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

11 REPLIES 11
Marsha_R
Community Hero

Re: breaking out of loop iteration

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

 

 

loop (i=1) {

 

  if (i != 3) {

  // do something

  }

 

  i++;

}

AlexKaras
Community Hero

Re: breaking out of loop iteration

Hi,

 

You may consider Label and Go To Label operations.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

Lee_M
Contributor

Re: breaking out of loop iteration

 

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 ?"

 

Lee_M
Contributor

Re: breaking out of loop iteration

 

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

 

Lee_M_0-1618302471840.png

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++;

}

Marsha_R
Community Hero

Re: breaking out of loop iteration

Marsha_R_0-1618326962390.png

 

AlexKaras
Community Hero

Re: breaking out of loop iteration

Hi,

 

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

Do you need to skip current iteration but remain inside the loop or you need to exit loop ?

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Lee_M
Contributor

Re: breaking out of loop iteration

Hi I need to exit the current iteration

 

I have found I can do it with 2 actions, but there has to be an easier way like 'break'

 

Lee_M_0-1618396659308.png

 

AlexKaras
Community Hero

Re: breaking out of loop iteration

Hi,

 

I still do not understand what you'd like to get, so pseudo-code for two options that I have in my mind (I hope that translation into keyword operations will be easy):

Option 1:

Do ' data-driven loop start

  If (<some condition>) Then ' if condition is not met then current loop iteration is effectively skipped and code proceeds to the next data-loop value

    <operations>

  End If

Until ' end of data-loop

...

 

Option 2:

Do ' data-driven loop start

  If (<some condition>) Then ' if condition is met then code exits from data-loop

    GoTo Label <aLabel>

  End If

Until ' end of data-loop

:Label <aLabel>

...

 

Does this help?

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Lee_M
Contributor

Re: breaking out of loop iteration

Alex,

 

I am looking to do option 1

 

Apologies, I am noty trying to do the javascript 'break' but instead, the 'continue' as per the example below

 

https://www.w3schools.com/js/tryit.asp?filename=tryjs_continue

 

I am using the .next and goto label (beginning of loop) to the same effect but was wondering if there was a way this could be done in one operation

New Here?
Join us and watch the welcome video:
Announcements