Create a While loop

A While loop runs its body until a condition tells it to stop. The number of iterations isn't fixed in advance. Use a While loop for iterative or convergence algorithms. Unlike a For-Each loop, a While loop has no fixed list of items to process. For a full explanation of how While loops work, see For-Each and While loops.

  1. Click Add node add node icon on the toolbar or press Space.

    The Add node dialog opens.

  2. Expand Blank subgraphs and select While subgraph.

    The While node is placed on the canvas.

  3. Connect a number to the Max Iterations input port on the While node.

    Max Iterations is a safety cap. If the Break condition never becomes true, the loop stops after this many iterations. Always wire this port to prevent runaway loops.

  4. Double-click the While node, or right-click it and choose Enter subgraph, to open the loop body.

  5. Inside the loop body, use the ports on the Subgraph Input node to access loop state:

    • Index: The current iteration number, starting at 0
    • LoopResults: The list of results from all previous iterations
  6. Wire your loop logic, then connect to the Subgraph Output node:

    • Loop Result: The value produced this iteration. It's appended to LoopResults for the next iteration.
    • Break: A Boolean that stops the loop when true. Wire a condition here.
  7. To carry a value from one iteration to the next, use a GetLastItem node on LoopResults to read the previous result and pass the new result to Loop Result.

    On the first iteration (Index equals 0), LoopResults is empty. Use an If node to check whether Index equals 0. When it does, provide a seed value. Otherwise, read from LoopResults.

Note: The length of LoopResults equals the number of iterations that ran. If the iteration count itself is what you need, read it from the parent graph using a GetListSize node on the LoopResults output.