> For the complete documentation index, see [llms.txt](https://docs.flowstem.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flowstem.app/commands/control-flow.md).

# Control flow

Control-flow commands decide which commands run and how often they run. Indented commands belong to the nearest open block, which is closed with **End**.

## Loop

Repeats the enclosed commands a fixed number of times.

* **Configure:** Number of repetitions and an index variable.
* **Output:** The index variable identifies the current repetition.
* **Example:** Repeat a form action five times using `$loop_index`.

## Loop Elements

Repeats the enclosed commands once for every page element matching a selector.

* **Configure:** List selector, item variable, visibility requirement, maximum items, and optional loading behavior.
* **Loading:** Flowstem can scroll or click a link or button to load more elements between scans.
* **Output:** The item variable represents the current matched element and supports relative selections inside the loop.
* **Example:** Loop through result cards and extract each card's name and URL.

Set a maximum item count when a page can load continuously.

## While

Repeats the enclosed commands while its conditions remain true.

* **Configure:** One or more conditions and a maximum number of iterations.
* **Conditions:** Test page visibility, text, numbers, empty values, or custom code; combine conditions with AND or OR.
* **Example:** Keep clicking **Load more** while the button remains visible, up to 20 times.

The maximum iteration count prevents a condition from creating an endless loop.

## If

Runs the enclosed commands only when its conditions are true.

* **Configure:** One or more conditions joined with AND or OR.
* **Conditions:** Test element visibility, empty values, text comparisons, number comparisons, or custom code.
* **Example:** If `$status` equals `Ready`, click the **Process** button.

## Else

Starts the fallback branch for the nearest **If** command.

* **Configure:** No command-specific fields.
* **Use it for:** Commands that should run only when the preceding If conditions are false.
* **Example:** Log `Not ready` when the If branch does not run.

Place Else after the If branch and use one End after the Else branch.

## End

Closes the nearest open If, Else, While, or Loop block.

* **Configure:** No command-specific fields.
* **Example:** Place End after all commands that belong inside a loop.

Flowstem validates block structure before a workflow runs.

## Stop Loop

Immediately exits the current loop and continues after its End command.

* **Configure:** No command-specific fields.
* **Use it for:** Ending a loop once a desired record or state has been found.
* **Example:** Stop a Loop Elements block after finding a card whose name matches `$target_name`.

Stop Loop must be inside Loop, Loop Elements, or While.

## Sub-Workflow

Runs another saved workflow, then returns to the next command in the current workflow.

* **Configure:** Workflow to run.
* **Variables:** The child workflow participates in the same run and can use the run's variables.
* **Example:** Reuse a saved **Sign in** workflow before several different tasks.

{% hint style="info" %}
Sub-Workflow requires the Desktop plan. It shares the parent workflow's runtime and safety deadline, and recursive workflow chains are rejected.
{% endhint %}
