> ## Documentation Index
> Fetch the complete documentation index at: https://docs.blobhub.io/llms.txt
> Use this file to discover all available pages before exploring further.

# For Component (logic.for)

> A counting loop: each entry counts from one value toward another and routes next or complete.

A `logic.for` counts. Each time the flow enters it, it moves a counter one step and sends the flow down `next`, to run
the loop body again, or `complete`, when the count is done. It holds no body of its own: the body is the steps after
`next`, and a loop is made by wiring the last of them back to the `logic.for`
([Wiring a loop](#wiring-a-loop)).

| Route | Category | Value | Notes |
| :- | :- | :- | :- |
| `next` | Flow output | — | Another round: the body runs, then returns to the `logic.for`. |
| `complete` | Flow output | — | The count is done. The flow leaves the loop. |
| `failure` | Flow output | — | `from`, `to` or `step` reads empty ([When it fails](#when-it-fails)). |
| `from` | Data input | `integer`, default `0` | Required. The first value of the counter. |
| `to` | Data input | `integer`, default `10` | Required. The bound, which the counter stops short of. |
| `step` | Data input | `integer`, default `1` | Required. What the counter moves by. A negative step counts down. |
| `current` | Data input | `integer`, default `0` | Required. The counter, read on entry. With no value it is `from - step`. |
| `current` | Data output | `integer` | Required. The counter after this entry, `current + step`. |

## How it counts

```mermaid theme={null}
graph LR
    A[logic.for] -- next --> B[loop body]
    B --> A
    A -- complete --> C[after the loop]
```

On each entry the `logic.for` reads `current`, decides the route, and writes `current + step` to the output `current`.
It posts a `component.output` event, `Loop step status: from (0) to (3) step (1) current (-1)`.

* **`next` while the counter is short of `to`.** With a positive `step`, `next` is taken while `current` is below
  `to - step`. With a negative `step`, while it is above `to - step`. Otherwise the route is `complete`.
* **The body sees the value the counter just reached.** The output `current` is written on every entry, `complete`
  included, so a body that reads it sees `from`, `from + step`, and so on, and never `to`. The flow after `complete`
  sees `to`, or the first value past it when `step` does not divide the range.
* **A fresh counter starts one step early.** With no value in `current`, the counter is taken as `from - step`, so the
  first entry writes `from`. A counter that already holds a value, such as a `0` the editor wrote, starts from that
  value instead.
* **It counts the way `range(from, to, step)` does.** `from 0 to 3` runs the body for `0`, `1` and `2`. `from 3 to 0`
  with `step` `-1` runs it for `3`, `2` and `1`.

## Wiring a loop

The counter has to survive between entries, so `current` is wired to a `data.integer` with the `execution`
persistence ([Persistence](https://docs.blobhub.io/blob-types/workflow/workflows/component-data.md#persistence)), once as an input and once as
an output, both with the route `current`. The `data.integer` holds the value the `logic.for` wrote last, and its inline
`value` is what the first entry reads. A `current` port that is wired, or holds a value, satisfies both the input and
the output.

The flow is a cycle: the `logic.for`'s `next` leads to the body, and the last step of the body leads back to the
`logic.for`'s flow input. A connection back is a normal connection. To jump to an earlier step from the middle of the
body, use a [`flow.goto`](https://docs.blobhub.io/blob-types/workflow/workflows/component-flow#flow-goto).

## When it fails

A `logic.for` whose `from`, `to` or `step` reads empty at run time, such as one a data component has not yet written,
takes `failure`, and posts no event. A definition that leaves any of the five required ports empty fails [its
check](https://docs.blobhub.io/blob-types/workflow/operations/check-definition.md) with ``Port `<route>` (category: `<category>`) is required``.

## Example

Ostrava Pumps sends a reminder for each of the three open service requests. A `data.integer` called `Round` holds the
counter, with the inline value `-1` and persistence `execution`, so the first entry writes `0`. The `logic.for` has
`from` `0`, `to` `3` and `step` `1`; `current` is wired to `Round` twice, as an input and as an output.

```json theme={null}
{
  "components": [
    {"id": "round", "category": "data", "type": "data.integer", "name": "Round",
     "position": {"x": 300, "y": 300},
     "ports": [
       {"category": "data_input", "route": "value", "value": {"type": "integer", "integer": -1}},
       {"category": "data_input", "route": "persistence", "value": {"type": "choice", "id": "execution"}}
     ]},
    {"id": "loop", "category": "processor", "type": "logic.for", "name": "Each request",
     "position": {"x": 300, "y": 100},
     "ports": [
       {"category": "data_input", "route": "from", "value": {"type": "integer", "integer": 0}},
       {"category": "data_input", "route": "to", "value": {"type": "integer", "integer": 3}},
       {"category": "data_input", "route": "step", "value": {"type": "integer", "integer": 1}}
     ]}
  ],
  "connections": [
    {"id": "c1", "source_id": "loop", "target_id": "round", "route": "current",
     "source_port": "data_input", "target_port": "data_reference"},
    {"id": "c2", "source_id": "loop", "target_id": "round", "route": "current",
     "source_port": "data_output", "target_port": "data_reference"},
    {"id": "c3", "source_id": "loop", "target_id": "<reminder step>", "route": "next",
     "source_port": "flow_output", "target_port": "flow_input"},
    {"id": "c4", "source_id": "<reminder step>", "target_id": "loop", "route": "success",
     "source_port": "flow_output", "target_port": "flow_input"},
    {"id": "c5", "source_id": "loop", "target_id": "<summary step>", "route": "complete",
     "source_port": "flow_output", "target_port": "flow_input"},
    {"id": "c6", "source_id": "loop", "target_id": "<end>", "route": "failure",
     "source_port": "flow_output", "target_port": "flow_input"}
  ]
}
```

The reminder step reads `Round` to learn which request it is on. The summary step runs once, after the third round.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.