# Flow Builder - Utilising Loops

## Working with FOR Loop Node

In many scenarios, you need to iterate over a collection of items and perform specific actions for each item. This is where FOR loop nodes become essential in Cosmocloud's flow builder.

Imagine you have a list of marks scored by a student and you want to calculate the total marks scored by the student. Here's how you can do it in Cosmocloud's flow builder:

### Assumptions

* You already have an API configured and a request body model configured with it which accepts `marks` as an array of floats.

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXfURVsSp0Ic_8TR-TjMoeM_1g5x9pLLN77kRHqPvWN1G3JqDUfUDPr1X3LLQQY6FFs1hiIOSMim3QSWoFD-cYihTpvVJGl2LyfnjY883vmUclagl-8SzopldZ1HslZi2Teqv7yrwB9oXMKNFdWyTpyOHj2l?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

* To learn about how to set up APIs [click here](https://docs.cosmocloud.io/resources/apis).
* To learn about how to set up a request body model[ click here](https://docs.cosmocloud.io/resources/models/building-models)

Now, let's  see how we can leverage the for loop node for iterating through the array of marks and getting the total marks of the student.

1. Click Add Node, and select the `Length of Array` node. This will help us get the length of the array which will be useful for iterating through the loop. Configure this node with a variable name of `len` and pass in the array for which we need the length, as shown in the figure below.

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXfWqy4L9r-L1DdxLo9qI3NUm4M4hnwJuaAw7ErE-bv-Nrcby3G_G_Z7_d556x73CHbfety5zWzZDRD478hiMeQm9Ut5_NG723IJqc8mvDxzblcUwl5sSqbIg9PUVjakm8GRfp0zDPX3CjhyxDRT2QeapYO7?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

2. Now lets add a `Set Variable` node, this variable will be our iterator and help us fetch the marks at different positions in the array. Configure this node as shown in the figure below.

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXenY6FzDTcYVlFkMerARM0urvnpRcb_YWKTTVl2xA01ADZoVFlCVc5km9N3gN4HrnNU3JJ9QBUAgqkbo2WpqBgzHuYMR2wAdJOgw3bFo50Ha6M21W071BFq9XB7q3YR495ejpjk6KfRaNWfZ1gin8fqgPPI?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

3. Now let's add the `For Loop` node to iterate through the marks array. To configure this node we need to specify the variable we will be using to loop through the array, the condition when we want to get out of the for loop and the increment factor by which our variable should be incremented after each iteration. Configure the `Variables to use` field and the `Increment factor` field as shown in the figure below.

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXddQDwyvCL1a-gSFyXkYYnbE6FDi0ujitiDzAOFZo1ftYD4lVAsKL9buCxMgMkAgFI8ciVBL1_TF8ECnyHVPchBLBgmlYYt6PING3LjwlorxHWcpWqbMyw9aiOfwWfT9ibv9ffJwCzg6nztP_TKUnantRY?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

4. Now let's add the condition, click on the `Edit condition` button and inside the JSON editor add the following condition.

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXe80PLx3tMAJdzcZij6yQKIaOUSO6wcHXG7A9gnk4N7t-zZzq2claXrZ8wIvEY6zS7KY2fVS3v79ljHomt7dCW2rXIwM-u4pSxoaDvN892AwyCUMNOLOUvz5CB0irfLr3ewGDUgT2cqbxJPV82yaBIG9c0T?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

5\. Let's get inside the for loop click on the `Get inside` icon. You now are inside the for loop's flow and we will now create a flow here which we want to execute inside the for loop.

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXdh4MA5QMAd0NLOJvtQUb-NcRR9uktoe8Ga5PToP6idvOFa7n3bBFdM4WX4abBDa2s2R0SuJZ4_BEPr1On3EfMuBKXHuKqMCuX0ybcQec8wLcJi62a4tqBxj3KpYZJyEfrwSCneuudvjDngtsGzfA_lrrg?key=3g19iCAD9aT3BbvyxuQRkw" alt="" width="375"><figcaption></figcaption></figure>

6. Here we first use the `Get Array Item` node to get the item at a particular index, let's name it item.

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXdCDh9_YoibHjuB7Xe4CJpEtTsWTQ43faFuAikN8d5l42j8e1WfMG27tosQ_wfRCUp__xII1CtagYCWRU4h4BYLnkhpXh9c5nuGsz_Ym3E09098AVeKBEZqKWNSEqlvCNgExQEIfRlsPZnLabtqGxhUM8f4?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

7. Now that we have the item, let's add it to our variable total using the \`Add Variable\` node.

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXdbCqkwo19L_BBYcahU8Phv9CUr0AVokloePd9oNZ7qsIG1D4dU8gkw9t5W0WHAV5Q7a0mYYbVJfwFjgXY_SpAF8RcnghFOjeS_YUpy0LMmNw-YC8J8RRA5OmcnV25Om2RrFahpOXs0qSBFhk9qBINArqkC?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

8. &#x20;This is what our flow inside the for loop look like:

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXeFEsAScce16VMy9VLS95GofTAEdsXC9lCzpeyVNMNZRD5aI4sbuU0MWvauoDFejBYTpVg1AioL9hFm1dky7pqWHBL5IaPrJrG2I2Y99jCoZDMpmvtPip5m4O7HNSnaYSCjAgXFGRi93F5R0qATqBCcVZqq?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

9. Finally, let's return the total marks scored by the student by configuring the \`HTTP Response\` node as follows:

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXdU_PdS-VyUEUwXOp9M6AEcvpi95I3Ft2qDTKiAs3g5Q8w4jrEX8UYNN7nKhV7M5O0XG7Pt6fRq-WMM9XxxWQ-V2bkippnOEAprSRqG24phu0p5PqWR_6vnxa_5BmHAGss8wvb61xb3QbUhZCjKr3ZSKmVd?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

10. This is what our entire flow look like:

<figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXdjf_LpcDG8r4sGdRCuSGDHk6nFI8vptGBf60He9m0Z-CDuDQRrnxmbwXYqTzvapA96Z2j7XwYf9OP_UNcVbM202Zamie94tmCVIYGRWV7SMa5tGsWVo6zGE7nmezqkNG8IigDoLcrpPDwcjA4j0g7upNzA?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

11. Let’s now hit this API with postman.<br>

    <figure><img src="https://lh7-us.googleusercontent.com/docsz/AD_4nXcpTQORAEyRc4jqQHv8XIG2fjtcfh93cE4DPV4t0d2kO8dPWiUrHr0qCFLLDk58naAjotDwCvEbg4rp_cWRjUHscVUwFmfOc32L0MiM8GlhON4FVLjniEHDoImsg86NA0iR7Hj4luQqd-iAM1j-p_B-Kx3q?key=3g19iCAD9aT3BbvyxuQRkw" alt=""><figcaption></figcaption></figure>

To learn more about the For Loop node [click here](https://docs.cosmocloud.io/flow-builder/node-types/loop-nodes)

<br>
