585
Views
5
Comments
Differnce between For Each and  ad-hoc loop ?
Application Type
Traditional Web

I am new to Outsystems and having confusion about when we need to use for each loop and when to ad-hoc ? 

What is main differnce between For Each and  ad-hoc loop ?

UserImage.jpg
Kay Lun

ForEach need a list to loop through each item in the list.

ad-hoc loop just like a normal for each, which you could specific how many times it should loop and do the action.


Sample 1:

Order Item Attributes : Id, OrderId, ProductId, Quantity

You have a list fetch from the aggregate, let's say an Order Item List which belong to an order, and you want to know the total sum of this order, you could use ForEach to loop through each Order Item, and do the calculation which sum the Product.Price * Quantity.


Sample 2:

You have a number given which is N=10

And you need to know the sum from 1 to N, then you need to use the ad-hoc loop by using If(), and calculate the sum, result = result + N.


2020-01-08 08-43-00
Rahul Kumar

Hi Bilal,

The main difference is, In for each loop you are  traversing  items of List. It will go through each item one by one. 

But In ad hock loop you will run loop using If  condition. Loop terminates when Condition fails.


Hope this help.


2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Muhammed,

In addition to what has already been said, if you use a For Each loop with the output of an Aggregate, and you do not use the Aggregate as output, or in a ListAppendAll etc., the Aggregate will use a database cursor, which means that each iteration a record is fetched. This is an optimization that speeds things up, but also means that when debugging you can only see the Current record.

Another difference: when looping over a list, you cannot modify that list itself (though you can modify the records the list holds), so no ListAppend, ListDelete etc. That is because a For Each is done (if it's not a database cursor like I described above) with an iterator, and you cannot modify a list that's being iterated.

Also, ad-hoc loops need some index variable, and if you are looping over a list you need to index all uses of that list, which is error prone and gets laborious quickly. So my advice is to always use a For Each, unless you really want to do something that makes it impossible (like deleting records from the list you're looping over, or inserting records into it).

EDIT: Just thought of another difference: you can only exit a For Each when all records have been looped over, or when you exit the entire action. It's not possible to "break" out of a For Each. So if you need to stop processing as soon as you reach a certain record, you may also consider using an ad-hoc loop (the alternative is setting some "skip" boolean that you test each iteration to see if further processing is needed, but this will still iterate over the entire list, which may be slow if it's a long list).

2024-10-03 19-36-33
Cemal Arik

Hi Muhammed Bilal, 

For Each:

  • It’s like a pre-made loop.
  • You use it to go through every item in a list, one by one.
  • Example: If you have a list of fruits, it will look at 
    Apple, then Banana, then Cherry, automatically.

Ad-hoc Loop:

  • You make the loop yourself using logic (like While or If).
  • It’s for special cases, like stopping early or skipping items.
  • Example: Go through a list of fruits, but stop when you see Banana.

  • Example:
    1. For Each Example:

      • List = [Apple, Banana, Cherry]
      • Output: Apple, Banana, Cherry (it shows all).
    2. Ad-hoc Loop Example:

      • List = [Apple, Banana, Cherry]
      • Stop at Banana.
      • Output: Apple, Banana (it stops there).
  • This way, For Each is easy and automatic. 
    Ad-hoc is for when you want more control.

    Summary:

    • Use For Each for standard list processing.
    • Use ad-hoc loops for custom, flexible iteration logic.

      Hope this helps. Good luck!
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Cemal,

You replied to a post that's 1,5 years old. Also, your answer has already been given in the other replies. Please check the date of the original question and any answers when replying to a post, thanks!

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