# Branch with a Condition

import { Steps } from "@astrojs/starlight/components"

A **Condition** step chooses one outgoing path among several. You define **IF** / **ELSE IF** rules (and an **Else**
fallback); Studio evaluates them in order and follows the first branch whose rule is true. Screenshots on this page use
a sample severity check so you can see branch names and Compare rules filled in.

## When to use

- A previous step returns a value (status, severity, flag) and later steps should differ based on that value.
- You want one flow that covers several cases instead of separate flows per case.
- Prefer [Handle step errors](../error-handling/) when the branch is about a failed service step (**On failure** →
  **Continue execution**) rather than a business rule on a successful payload.
- Prefer [End a flow with Success or Failure](../end-a-flow-with-success-or-failure/) when a path should stop the run as
  successful or failed.

Before you begin, open the flow in the designer with permission to edit it. Place the Condition after the step that
produces the value you will compare.

## Guide

### Add a Condition

**Condition** lives under **Flow**. It can have several outgoing branches; most other steps allow only one.

<ol>
<li>

**Add Condition.** Open **Add a step**, choose **Flow**, then **Condition**. Place it after the step that produces the
value to test. Rename the step for the rule.

<div class="docs-screenshot-compact">

![Canvas with Transform data into Condition, then Success and Failure branches](./assets/canvas-transform-data-into-condition-then.png)

</div>

</li>
<li>

**Open Conditions.** Select the Condition step and open the **Conditions** tab. Each row is an **IF** or **ELSE IF**
with a branch name, **THEN** (target step), and an **Edit** control for the Compare rule. The last row is **Else**.

<div class="docs-screenshot-compact">

![Conditions tab with IF true THEN Success and ELSE IF false THEN Failure](./assets/conditions-tab-if-true-then-success.png)

</div>

</li>
</ol>
### Name branches and set targets

Branch names appear on the canvas links. **THEN** picks which step runs when that rule matches. **Else** runs when no
rule matches.

<ol>
<li>

**Name each branch.** In the name field (placeholder like `#1: Condition name`), type a short label. That label shows
on the canvas link.

</li>
<li>

**Choose THEN.** In the **THEN** select, pick the next step for that branch. Connect new steps from the Condition if
the target is not listed yet.

</li>
<li>

**Set Else.** On the **Else** row, choose the fallback step (or leave it empty only while you still build the path).

</li>
</ol>
### Edit the Compare rule

**Edit** opens the formula editor for that branch. Use **Compare** with **Operand A**, **Operator**, and (when
needed) **Operand B**. Studio evaluates branches top to bottom; the first true rule wins.

<ol>
<li>

**Open Edit.** On the branch row, choose **Edit**. The editor title shows the branch name (or a default such as
**Condition #1**).

</li>
<li>

**Build the comparison.** Set **Operand A** from an earlier step (data picker). Choose an **Operator**. Fill
**Operand B** when the operator needs a second value (for example **Is equal to**). Unary operators such as
**Is true**, **Is false**, **Is empty**, and **Is not empty** do not use **Operand B**.

<div class="docs-screenshot-compact">

![Compare editor with Operand A, operator Is not equal to, and Operand B](./assets/compare-editor-operand-operator-not-equal.png)

</div>

</li>
<li>

**Chain rules when needed.** Choose **+ Add comparison**, then set **Chaining Operator** to **And** or **Or** so
several comparisons must all match or any one can match.

<div class="docs-screenshot-compact">

![Several Compare rows linked with And or Or](./assets/several-compare-rows-linked.png)

</div>

</li>
</ol>
### Reorder branches and use Else

Order matters: earlier rules run first. You can move a branch, promote a path to **Else**, or delete a branch from the
row menu.

<ol>
<li>

**Open the branch menu.** On a condition row, open the overflow menu.

</li>
<li>

**Reorder or promote.** Choose **Move up** or **Move down** so the most specific rule is first. Choose **Use as else**
to make that path the fallback, or **Delete** to remove the branch.

<div class="docs-screenshot-compact">

![Branch overflow menu with Move up, Move down, Use as else, and Delete](./assets/branch-overflow-menu-move-up-move.png)

</div>

</li>
</ol>
### Available operators

These are the **Operator** values on **Compare**:

| Operator                                         | Typical use                             |
| ------------------------------------------------ | --------------------------------------- |
| **Is equal to**                                  | Exact match                             |
| **Is not equal to**                              | Value differs                           |
| **Is true** / **Is false**                       | Boolean from a previous step            |
| **Is empty** / **Is not empty**                  | Missing vs present string or collection |
| **Is greater than** / **Is greater or equal to** | Numbers                                 |
| **Is lower than** / **Is lower or equal to**     | Numbers                                 |
| **Contains** / **Does not contain**              | Substring inside a string               |
| **Starts with** / **Ends with**                  | Prefix or suffix                        |
| **Matches regex**                                | Pattern such as only lowercase letters  |

## Example

A flow named **Route severity check** transforms an alert, then uses **Condition** so critical alerts end on
**Success** and others on **Failure**.

1. **Add Transform data** ahead of the Condition so the severity (or other field) is easy to pick.
2. **Add Condition** with an **IF** branch named for the pass case (**THEN** → Success) and **Else** (or a second
   branch) → Failure.
3. **Edit** the pass branch: **Operand A** = severity from Transform data, **Operator** = **Is equal to**,
   **Operand B** = the critical value your tool returns.
4. **Run** the flow once with sample data and confirm the canvas follows the expected branch.

### Read evaluation in the run log

After a run, open the Condition step in **Logs**. The **Conditions** view shows each compared value and which branch matched, so you can see why the run took that path.

<div class="docs-screenshot-compact">

![Condition canvas with named branches and run log Conditions inspector showing Matched and Selected on the big branch](./assets/condition-evaluation-run-log.png)

</div>

## Keep in mind

- Branches are evaluated in list order; only the first matching branch runs.
- **Else** is the path when no **IF** / **ELSE IF** rule matches—not a second “false” Compare you must maintain.
- Comparators are case-sensitive for string equality unless you normalize the value first (for example with a transform
  function).

Related: [Handle step errors](../error-handling/),
[End a flow with Success or Failure](../end-a-flow-with-success-or-failure/),
[Scripts, errors, and run control](../).
