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

# Control Flow

> Conditional branching with if/then/else in Replay workflows.

# Control Flow

The `if` step evaluates a condition and executes either the `then` or `else` branch. It is how you model data-dependent logic in your workflows.

## Basic If/Then

```yaml theme={null}
- name: check-status
  type: if
  condition: ["status_code", "==", 200]
  then:
    - name: log-success
      type: print
      message: "Request succeeded"
```

If the condition passes, the `then` steps run. If it fails, the step passes silently (no error).

## If/Then/Else

Add an `else` block to handle the failure case:

```yaml theme={null}
- name: check-status
  type: if
  condition: ["status_code", "==", 200]
  then:
    - name: success
      type: print
      message: "Request succeeded"
  else:
    - name: failure
      type: print
      message: "Request failed with status {{ status_code }}"
```

## Condition Format

The condition is a three-element array: `[value_or_path, operator, expected_value]`.

### Using State Variables

```yaml theme={null}
- name: route-by-role
  type: if
  condition: ["user_role", "==", "admin"]
  then:
    - name: admin-panel
      type: http
      request:
        method: GET
        url: /admin/dashboard
  else:
    - name: user-profile
      type: http
      request:
        method: GET
        url: /users/{{ user_id }}/profile
```

The first element refers to a variable in the state bag. The third element can be a literal or another state variable.

### Using JSONPath on Results

```yaml theme={null}
- name: check-response
  type: http
  request:
    method: GET
    url: /health
  extract:
    healthy: $.data.status

- name: handle-status
  type: if
  condition: ["healthy", "==", "ok"]
  then:
    - name: all-good
      type: print
      message: "System healthy"
  else:
    - name: alert
      type: shell
      command: "echo 'System unhealthy!'"
```

## Operators

| Operator   | Aliases    | Type    | Description                    |
| ---------- | ---------- | ------- | ------------------------------ |
| `eq`       | `==`, `=`  | Any     | Equal (type-aware for numbers) |
| `ne`       | `!=`, `<>` | Any     | Not equal                      |
| `gt`       | `>`        | Numeric | Greater than                   |
| `lt`       | `<`        | Numeric | Less than                      |
| `ge`       | `>=`       | Numeric | Greater than or equal          |
| `le`       | `<=`       | Numeric | Less than or equal             |
| `contains` |            | String  | Target contains substring      |
| `in`       |            | Array   | Value exists in array          |

### Operator Examples

```yaml theme={null}
# Numeric comparison
condition: ["count", ">", 10]

# String containment
condition: ["email", "contains", "@"]

# Value in array
condition: ["role", "in", "admin,moderator,editor"]

# Not equal
condition: ["status", "!=", "error"]

# Greater or equal
condition: ["score", ">=", 100]
```

## Nested If Steps

You can nest `if` steps inside `then` or `else` blocks:

```yaml theme={null}
- name: nested-logic
  type: if
  condition: ["authenticated", "==", true]
  then:
    - name: check-permission
      type: if
      condition: ["role", "==", "admin"]
      then:
        - name: grant-access
          type: print
          message: "Admin access granted"
      else:
        - name: limited-access
          type: print
          message: "Limited access granted"
  else:
    - name: reject
      type: print
      message: "Authentication required"
```

## Combining with Extract

A common pattern: extract a value, then branch based on it:

```yaml theme={null}
- name: fetch-order
  type: http
  request:
    method: GET
    url: /orders/{{ order_id }}
  extract:
    order_status: $.data.status
    order_total: $.data.total

- name: process-by-status
  type: if
  condition: ["order_status", "==", "pending"]
  then:
    - name: approve
      type: http
      request:
        method: PUT
        url: /orders/{{ order_id }}/approve

    - name: notify
      type: print
      message: "Order {{ order_id }} approved"
  else:
    - name: skip
      type: print
      message: "Order {{ order_id }} is {{ order_status }}, skipping"
```

## What's Next?

* Iterate over arrays with [loops](/replay/guides/loops)
* Compose workflows with [workflow chaining](/replay/guides/workflow-chaining)
