For Loop
The For Loop node allows you to iterate over a range of integers, executing a set of nodes for each value in the range. It’s useful when you need to repeat a series of actions a specific number of times.Inputs
flow(required): The entry point for the loop. Connect this to the node that should run before the loop begins.startIndex(optional): The integer value at which the loop should begin. Default is 0.endIndex(optional): The integer value at which the loop should end (inclusive). Default is 0.
Outputs
loopBody: Connect this to the first node inside the loop. These nodes will execute once for each value in the range.index: Outputs the current integer value in the range on each iteration.completed: The exit point for the loop. Connect this to the node that should run after the loop finishes.
Configuration
This node has no additional configuration options.Example Usage
Here’s an example of how to use the For Loop node to sum the numbers from 1 to 10:- Add a For Loop node to your spell.
- Connect a Trigger or other entry point node to the
flowinput. - Set the
startIndexto 1 and theendIndexto 10. - Add an Add node inside the loop and connect the For Loop’s
loopBodyoutput to it. - Connect the For Loop’s
indexoutput to one of the Add node’s inputs. - Connect the Add node’s output to a variable to accumulate the sum.
- Connect the Add node’s output back to its own second input. This will cause each iteration to add the current index to the running total.
- Connect the For Loop’s
completedoutput to any nodes that should run after the loop finishes.
Best Practices
- Be mindful of off-by-one errors. The loop is inclusive of both
startIndexandendIndex. - Avoid infinite loops. Make sure your
endIndexis reachable from yourstartIndexbased on the logic inside your loop. - If you need to loop through an array rather than a range of numbers, consider using the For Each node instead.
Common Issues
- Forgetting to connect the
completedoutput can cause your spell to hang after the loop finishes. Always make sure your loop has an exit path. - Connecting a node’s output directly to its own input without an accumulator variable will overwrite the value on each iteration instead of accumulating. Use a variable node to store running totals or other accumulated state.
