# How control flow works

> **About this page**
>
> - Kind: Explanation
> - Summary: A workflow's tasks are grouped in blocks, and if and for_each tasks run a block inside themselves; this is how outputs and failures cross blocks.
> - Last changed: 2026-11-17
> - Page: https://docs.swale.io/explanation/how-control-flow-works
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.

A workflow's tasks live in named blocks, and a run starts in the block its `entry_point` names, `main` by default. Control tasks run other blocks inside themselves: an if task enters the `true` or `false` block that matches its condition, if it names one, and a `for_each` task runs a block once for each item in a list.

```mermaid
flowchart TB
  accTitle: How a run moves between blocks
  accDescr: The run starts in the main block, the entry_point. There, Step one starts first, and the if task Router and the for_each task Each file start once Step one is Complete. Router enters the fast_path block when its condition is truthy and the slow_path block when it is falsy. Each file runs the per_file block once for each item in its list.
  subgraph main["Block <code>main</code>, the entry point"]
    stepOne("Step one")
    router("Router<br/><code>if</code>")
    eachFile("Each file<br/><code>for_each</code>")
    stepOne -->|<code>start_on</code>| router
    stepOne -->|<code>start_on</code>| eachFile
  end
  fastPath("Block<br/><code>fast_path</code>")
  slowPath("Block<br/><code>slow_path</code>")
  perFile("Block<br/><code>per_file</code>")
  router -->|truthy| fastPath
  router -->|falsy| slowPath
  eachFile -->|once per item| perFile
```

How the example below moves between blocks. Tasks in a block start as soon as it is entered and there is room for them, unless `start_on` makes them wait for other tasks to complete.

## Block model

A workflow definition organizes tasks into **named blocks**. Each block is a dictionary of tasks that can run as a group, and the `entry_point` field names the block the run starts in.

A run enters any other block only through a control task. The control task runs that block inside itself and completes once the block's tasks have, and then the tasks that wait on the control task continue, back in the block the control task belongs to.

```yaml
name: Control flow example
compute_type: cpu-4
inputs:
  mode:
    default: fast
entry_point: main
blocks:
  main:
    tasks:
      step_one:
        name: Step one
        uses: my-account/my-task@1.0.0
      router:
        name: Router
        start_on:
          - step_one
        if:
          condition: ${{inputs.mode == 'fast'}}
          true: fast_path
          false: slow_path
      each_file:
        name: Each file
        start_on:
          - step_one
        for_each:
          items: ${{tasks.step_one.outputs.files}}
          block: per_file
  fast_path:
    tasks:
      do_fast:
        name: Do fast
        uses: my-account/fast-task@1.0.0
  slow_path:
    tasks:
      do_slow:
        name: Do slow
        uses: my-account/slow-task@1.0.0
  per_file:
    tasks:
      transform:
        name: Transform
        uses: my-account/transform-task@1.0.0
        args:
          file: ${{tasks.each_file.for_each.item}}
```

## If tasks

An **if task** evaluates its condition once, when it starts, and enters the block for the outcome: its `true` block when the condition is truthy, its `false` block when it is falsy. When it names no block for the outcome, it completes without entering one, and the tasks that wait on it continue. Its execution log, and the run's Workflow Graph, show which branch it took.

## For Each tasks

A **`for_each` task** resolves its list when it starts and runs its block once per item, each iteration a copy of the block with its own item and index. The iterations run at the same time, as far as the account's limits allow, and the rest wait as **Pending** until there is room; see [Tasks that run at once](https://docs.swale.io/reference/limits-and-quotas.md#tasks-that-run-at-once).

The [Control flow reference](https://docs.swale.io/reference/control-flow.md) gives both tasks' fields and rules, and how they appear in the Workflow Graph.

## Outputs and blocks

A task reads the outputs of the tasks it waits on through `start_on`. Inside a block it can also read the outputs of the tasks the enclosing if or `for_each` task waits on, and each for_each iteration reads only its own copies of the block's tasks. Outputs stay inside their block: a task after an if or `for_each` task cannot read the outputs of the tasks inside it, so [export a value as an environment variable](https://docs.swale.io/reference/task-definition-syntax.md#exporting-environment-variables-to-later-tasks) to use it later in the run.

## Nesting

Blocks can contain further control tasks, so you can nest if and `for_each` tasks to any depth your workflow requires. A control task's block runs nested inside it, and the block's tasks can `start_on` only each other.

A failure inside a block is not contained by it: it fails every if and `for_each` task around it, and the run, as [Failure propagation](https://docs.swale.io/reference/run-and-task-lifecycle.md#failure-propagation) describes.

## Next steps

- [Control flow reference](https://docs.swale.io/reference/control-flow.md)
- [Use conditional logic in workflows](https://docs.swale.io/how-to-guides/use-conditional-logic-in-workflows.md)
- [Iterate over a list with for_each](https://docs.swale.io/how-to-guides/iterate-over-a-list-with-for-each.md)
