> For the complete documentation index, see [llms.txt](https://tchat.tect.host/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tchat.tect.host/general/actions/loops.md).

# Loops

Loop actions allow you to execute the same group of actions multiple times.

## \[FOR]

```yaml
actions:
- "[FOR] 5"
- "[MESSAGE] Hello!"
- "[ENDFOR]"
```

Executes all actions between `[FOR]` and `[ENDFOR]` the specified number of times.

In this example, the player receives **5** messages.

## \[ENDFOR]

```yaml
actions:
- "[ENDFOR]"
```

Marks the end of the loop.

Every `[FOR]` must have a matching `[ENDFOR]`.

## Nested Loops

```yaml
actions:
  - "[FOR] 2"

  - "[MESSAGE] Outer loop"

  - "[FOR] 3"
  - "[MESSAGE] Inner loop"
  - "[ENDFOR]"

  - "[ENDFOR]"
```

Loops can be nested.

Execution:

```
Outer loop
  Inner loop
  Inner loop
  Inner loop

Outer loop
  Inner loop
  Inner loop
  Inner loop
```

The outer loop runs 2 times, and during each iteration the inner loop runs 3 times, resulting in 6 executions of the inner message.

## Dynamic count

The number of iterations can be any expression supported by the expression engine.

```yaml
actions:
  - "[FOR] {arg0}"
  - "[MESSAGE] Hello!"
  - "[ENDFOR]"
```

If the player executes:

```
/example 4
```

The message will be sent 4 times.

## Example

```yaml
actions:
  - "[MESSAGE] <gray>Starting command for <yellow>{player}</yellow>..."

  - "[FOR] {arg0}"

  - "[MESSAGE] <green>Iteration #{arg0}"
  - "[SOUND] entity.player.levelup"

  - "[IF] {arg1} == 'fire'"
  - "[CONSOLE_COMMAND] effect give {player} fire_resistance 5 0 true"

  - "[ELSE IF] {arg1} == 'heal'"
  - "[CONSOLE_COMMAND] effect give {player} instant_health 1 1 true"

  - "[ELSE IF] {arg1} == 'speed'"
  - "[CONSOLE_COMMAND] effect give {player} speed 5 1 true"

  - "[ELSE]"
  - "[MESSAGE] <gray>No effect selected."
  - "[ENDIF]"

  - "[MESSAGE] <yellow>Arguments:</yellow> {args}"

  - "[ENDFOR]"

  - "[MESSAGE] <green>Loop finished successfully!"
```
