# repeat

Learn how to execute actions in a loop.

This action loops over a list of actions while a condition remains true.

For example, you can loop until an available filename is found or retry a check until a condition is met.

## Example

The following template finds an available directory name by appending an incrementing suffix:

**template.json5**

```json5
{
  "$schema": "https://schema.croct.com/json/v1/template.json",
  "title": "Available name finder",
  "description": "Finds an available directory name.",
  "actions": [
    {
      "name": "define",
      "variables": {
        "baseName": "my-project",
        "attempt": 0
      }
    },
    {
      "name": "locate-path",
      "path": "my-project",
      "limit": 1,
      "depth": 0,
      "result": "matches"
    },
    {
      "name": "repeat",
      "condition": "${this.matches.length !== 0}",
      "actions": [
        {
          "name": "define",
          "variables": {
            "attempt": "${this.attempt + 1}"
          }
        },
        {
          "name": "locate-path",
          "path": "${this.baseName}-${this.attempt}",
          "limit": 1,
          "depth": 0,
          "result": "matches"
        }
      ]
    },
    {
      "name": "print",
      "semantics": "info",
      "message": "Available name: ${this.baseName}${this.attempt > 0 ? '-' + this.attempt : ''}"
    }
  ]
}
```

To apply this template, run:

```sh
croct use template.json5
```

If `my-project` already exists, the loop increments the suffix until it finds an available name like `my-project-1`.

## Properties

These are the supported properties:

- `name`: `string`

  The action identifier. Always `repeat` for this action.

- `condition`: `boolean`

  An [expression](/reference/cli/templates/expressions) that evaluates to a boolean.

  The loop continues while the condition is true.

- `actions`: `Array<Action>` (see [Action](/reference/cli/templates/actions), [Array\<Action>](/reference/cli/templates/actions))

  The actions to execute on each iteration.

## Explore

- [Test](/reference/cli/templates/actions/test): Learn how to branch based on conditions.
- [Define](/reference/cli/templates/actions/define): Learn how to define the list to iterate over.
- [Write file](/reference/cli/templates/actions/write-file): Learn how to write a file per iteration.
- [Add component](/reference/cli/templates/actions/add-component): Learn how to add components in a loop.
