# When to Use Control Flow Bricks

The following table describes scenarios where you might use each type of control flow brick:

| Scenario                                                                                                 | Control Flow Brick                         |                                                                                                                                                               |
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| You need to run multiple bricks if a condition is met                                                    | If-Else                                    |                                                                                                                                                               |
| You need to run one brick(s) if a condition is met, and a different brick(s) if the condition is not met | If-Else                                    |                                                                                                                                                               |
| You are calling an API that sometimes fails                                                              | Retry                                      |                                                                                                                                                               |
| You want to wait for a condition to be met. For example: an API results to change                        | Retry                                      | In the Retry body, add a "Cancel Current Action" that runs if the condition has not been met yet (See Exceptions/Errors below)                                |
| You want to wait for an element to appear on the page                                                    | N/A - use the Wait for a DOM element brick | Trick question! The Wait for a DOM element brick has better performance because it can use native browser APIs to detect new elements                         |
| You want to show a custom error message/instructions to the user if a brick fails                        | Try-Except                                 | Leave the catch branch blank                                                                                                                                  |
| You want to ignore an error code when calling an API                                                     | Try-Except                                 | Leave the catch branch blank                                                                                                                                  |
| You want to perform an action for each item in a list of items returned from an API                      | For-Each                                   | <p>By default an element key is made available to the body <br><br>You can customize the name of the key provided to the body</p>                             |
| You want to preform an action for each element currently on the page that matches a selector             | For-Each Element                           | Current the Page Editor does not support selecting multiple elements. You must manually tweak the automatically generated selector to match multiple elements |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.pixiebrix.com/developing-mods/developer-concepts/control-flow/control-flow-bricks/when-to-use-control-flow-bricks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
