3405
Views
5
Comments
Solved
How to create a loop but not For Each loop?
Question

Hi, I'm having trouble with create a normal loop, OutSystems provides a For Each loop but it requires a record list to use but I don't really need it.


My job is create a list with n (n is a dynamic integer) items. I want to create a loop to iterate from 0 to n -1  then I can do a ListInsert(). Please help.

2022-05-06 16-37-40
Joseph Danne Enriquez
Solution

Hi Long!

You could use an If widget with the condition of a variable used as a counter (for example: Counter <= n-1). One branch contains the loop logic that goes back to the If widget, the other branch continues into the rest of the server/screen action. Set an assign before that If widget so that you can set the Counter to an initial value.

Kind regards,
Joseph Enriquez

2023-04-07 14-34-48
Logan Du

Joseph Enriquez wrote:

Hi Long!

You could use an If widget with the condition of a variable used as a counter (for example: Counter <= n-1). One branch contains the loop logic that goes back to the If widget, the other branch continues into the rest of the server/screen action. Set an assign before that If widget so that you can set the Counter to an initial value.

Kind regards,
Joseph Enriquez

Brilliant, why I can't think of If widget. Thanks for your support.


2018-10-04 11-30-51
glenn michiels

Hi Long,

As shown on the example below you could use an if widget with a count variable to create a loop.

Use an assign to reset the counter to 0 at every start then check your counter against Input (n) to loop through the inserts until the counter has reached Input (n).

Hope this helps!

2022-01-28 21-03-49
Felipe de Souza

Thanks! It helped me too. This "if loop flow" works like a 'while' statement. Simple, oldschool, but yet powerful.

UserImage.jpg
Makhaba Sennane

Thanks! I am doing a store app, where customers add items to their cart and then all those items are to be deleted and added as order items, thus this is very helpful. for each was not helping me achieve my goal, thank you very much.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.