# While Loop

Executes body nodes until the body returns `null`. Use it when the number of iterations is unknown and each iteration computes the next input.

## How It Works

The first iteration receives the node `input`. The body returns either:

- any non-null value to continue
- `null` to stop

Each non-null returned value is appended to `output` and becomes the next iteration input. Iterations are strictly sequential.

## Configuration

**max_iterations** (required): Maximum body executions before the node fails if the body never returns `null`. Must be at least 1.

This is a safety cap for pagination and polling workflows.

## Inputs & Outputs

**Input**: `input` (required) - Initial state for the first iteration.

**Outputs**:

- **output**: Array of all non-null body results.
- **loop**: Current iteration input. Connect this to the loop body entry node.

After completion, `loop` emits `null` so the body path does not run again.

## Stop Semantics

Only `null` stops the loop.

- The final `null` is not included in `output`.
- `false` continues because it is non-null.
- Strings, objects, arrays, numbers, `true`, and `false` all continue.
- Reaching `max_iterations` without `null` fails the node.

## Pagination Example

Initial input:

```json
{
  "cursor": null,
  "items": []
}
```

Body logic:

1. Fetch a page using `input.cursor`.
2. Append returned items to state.
3. If another page exists, return the next state:

```json
{
  "cursor": "next-cursor",
  "items": ["accumulated", "items"]
}
```

4. If there is no next page, return `null`.

The While Loop output is an array of all non-null intermediate states. Read the last item for final accumulated state, or perform side effects in the body and use the output as an audit trail.

## Tips

- Connect the `loop` output to at least one body node.
- Pick a conservative `max_iterations` for the external system.
- Branches that return `null` can stop the loop.
- Use Loop for known arrays and Constant Loop for fixed counts.

## Skipping an Iteration

A body that returns `skip(reason)` stops the loop, and the skip is not collected.

Unlike a counted loop, each iteration here is fed the previous iteration's value, so a body that declined to produce one leaves nothing to continue from. Use `skip(reason)` when the loop should end without contributing a final item; return `null` for the ordinary stop.
