Nodes/InitialB Util/๐Ÿ”„ For Loop End
ComfyUI Node

๐Ÿ”„ For Loop End

Why the For Loop isn't finished until you close it

By benjiyayaยทCreated 6 months agoยทUpdated 6 months agoยท 1
๐Ÿ”„ For Loop End
  • flow
  • initial_value1
  • initial_value2
  • initial_value3
  • initial_value4
  • initial_value5
  • value1
  • value2
  • value3
  • value4
  • value5

Every for loop in InitialB Util comes in a pair, and the End node is the half people skip until the loop stops working. InitialBForLoopEnd is what closes the Start node's flow, runs the loop for its total iterations, and hands back the final state. Without it, InitialBForLoopStart is just a weird node that outputs an index.

What it takes in

  • flow (required) - the FLOW_CONTROL wire from the matching For Loop Start. No flow, no loop; it's how the End knows which Start it belongs to.
  • initial_value1 through initial_value5 (optional) - the values coming back out of the loop body on each pass. These are what carry state around the loop.

Outputs are value1 through value5: the values after the loop completes - i.e. the result of the last iteration. If you accumulate something (a progressively refined latent, a counter, a summed total), this is where it comes out.

What it does mechanically

The For Loop Start doesn't actually run a for loop. It expands into a while-loop structure with an internal counter, and this End node is what builds the exit logic. When it fires, it:

  1. Looks up its paired Start node (via the dynamic execution graph) and reads back total.
  2. Expands into an index-increment (using the pack's Math Int node to add 1) plus a comparison - "index < total" - and feeds that into the underlying While Loop End.

In other words, the For Loop End is literally a while loop whose condition is "keep going while the counter is below total." That's the whole secret of this node, and it's why a For Loop Start requires an End: the End is where the iteration actually resolves. A loop with no End isn't a loop at all, it's an unclosed promise.

The gotchas that bite everyone once

  • The loop isn't done until the End fires. If you drag values out of the Start node's valueN outputs and read them downstream outside the loop, you're reading the initial values, not the results. The final values live on the End node.
  • index starts at 0. total=5 iterates 0โ€“4, not 1โ€“5. If your loop body uses the index in math (say, multiplying it), expect to be off by one until you adjust.
  • Read the loop's result from the End, not from inside. During the loop, everything inside the body is a transient copy (the mechanism clones the subgraph each pass, same as the While Loop End does). Only the End node's outputs are stable after completion.
  • Loop bodies must be inside the flow wire. Nodes placed between Start and End but not actually connected into that flow run exactly once. If an "inside the loop" node produces stale output, check its wiring.

When you'd use it

Same job as any for loop: run a subgraph a known number of times and collect the final state - N passes of an upscaler, N animation frames with the latent carried through, N iterations of a progressive refinement. When the number of passes is fixed, this pair beats the While Loop pair because the exit condition is guaranteed by total; you can't accidentally write a forever loop with a for loop.

Install

Pack: InitialB Util. Install via ComfyUI Manager (search "InitialB Util"), or:

cd ComfyUI/custom_nodes
git clone https://github.com/benjiyaya/Comfyui_InitialB_Util
cd Comfyui_InitialB_Util
pip install -r requirements.txt

Restart ComfyUI. No model files, no heavy deps - the requirements are torch/numpy/scipy/Pillow, already bundled. Two pack-wide notes: the README's clone URL is a placeholder, so use the real repo above or Manager; and loops need a ComfyUI with dynamic execution support (any build from mid-2024 on), so update ComfyUI if you're behind.

CategoryInitialB/flow/loop

Inputs (6)

NameTypeDefaultDescription
flowFLOW_CONTROLโ€”
initial_value1opt*โ€”
initial_value2opt*โ€”
initial_value3opt*โ€”
initial_value4opt*โ€”
initial_value5opt*โ€”

Outputs (5)

NameTypeDescription
value1*โ€”
value2*โ€”
value3*โ€”
value4*โ€”
value5*โ€”