Nodes/Basic data handling/if/elif/.../else
ComfyUI Node

if/elif/.../else

Real if/elif/else, without contorting your graph into a switch

By StableLlama·Created about a year ago·Updated 4 days ago· 48
if/elif/.../else
  • then
  • then_0
  • else
  • result
if
elif_0

This is the closest thing ComfyUI has to a real if/elif/.../else statement, and it's the node people recommend when a beginner asks "how do I route to different paths based on a condition?" - search the subreddit and it comes up in exactly those answers. It's the flow-control workhorse of Basic data handling, StableLlama's zero-dependency pack: a boolean if, a then value, any number of elif_0/then_0 pairs, and a final else. Pick the first condition that's true, hand back its value.

How it works

The node evaluates if first. If true, it returns then and stops. If false, it walks the elif pairs in order - elif_0 and its then_0, then elif_1/then_1, and so on - returning the first then whose condition is true. If nothing matched, it returns else.

The clever part is lazy evaluation. The node's check_lazy_status only asks ComfyUI to compute the branches it actually needs. If if is true, it requests then and never touches any elif or the else. If no condition matches, it requests only else. That means you can put expensive operations in the branch values (a whole image load, say) and the ones you don't take cost you nothing. This is the mechanism that makes conditional routing in ComfyUI practical instead of wasteful.

The inputs and the output

  • if (BOOLEAN) - the first condition. Wire it from a comparison node, a boolean operation, an IsNull - anything producing a real boolean.
  • then (ANY) - the value returned when if is true.
  • elif_0 (BOOLEAN) and then_0 (ANY) - dynamic pairs; add more with "+". Evaluated in order after if fails.
  • else (ANY) - the fallback when every condition is false.
  • result (output, ANY) - whichever branch value won.

Installing it

Install Basic data handling from ComfyUI Manager (search the pack title), or:

cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling

Restart ComfyUI. No Python dependencies, no models.

Troubleshooting

  • "It always picks the false/else branch." This is a real, reported issue with this family of nodes - someone on r/comfyui was burning through a workflow where if/else "seems to always be choosing the false branch." The usual culprits: (1) the condition input is forceInput, so it wants a wired BOOLEAN, not a toggle you eyeball - if you think it's connected but it isn't, it reads as false and you silently land in else; (2) the BOOLEAN feeding if isn't the value you think it is (a not node in between, or a comparison with the operands swapped). Trace the wire, confirm the boolean, and remember: with nothing true and no else connected, you get None.
  • "Two conditions are both true and it picks the wrong one." It picks the first true branch, top to bottom - that's if/elif semantics. Reorder to put the most specific condition first.
  • "I want a switch, not a ladder." If you're choosing by an index rather than conditions, that's the pack's switch/case node. Different tool.
  • Don't conflate with IsNull. If you want "when this value is missing, use a fallback," chain IsNull into if and put the fallback in then. The else branch is for "value exists," which is the opposite of what feels natural.
CategoryBasic/flow control

Inputs (5)

NameTypeDefaultDescription
ifBOOLEAN
then*
elif_0optBOOLEAN
then_0opt*
elseopt*

Outputs (1)

NameTypeDescription
result*