# /for

### Syntax

**/for** ***varname** initial-value* **to|downto** final-value **\[step** *interval***]**

**/next** ***varname***

## Description

This runs all commands between the /for line and the /next line, after which it increments/decrements the *varname* number (#1) by *step* number (#3) (default is 1) before running through the commands again. It will keep doing this until the *varname* number equals #2. You can end a /for loop immediately with /break or try the next iteration with /continue.

### Examples

**Simplest**

```
/declare varname int local
/for varname 1 to 5
    /echo ${varname}
/next varname


| Will output:
|  1
|  2
|  3
|  4
|  5
```

**Using /continue**

```
/declare varname int local
/for varname 1 to 5
   /if ({$varname} == 3) /continue
   /echo ${varname}
/next varname


| Will output:
|  1
|  2
|  4
|  5
```

**Using /break**

```
/declare varname int local
/for varname 1 to 5
    /if (${varname} == 3) /break
    /echo ${varname}
/next varname


| Will output:
|  1
|  2
```

**With a pre-defined ending variable**

```
/declare foo int local 5

/declare varname int local
/for varname 1 ${foo}
  /echo ${varname}
/next varname

| Will output:
|  1
|  2
|  3
|  4
|  5
```

### See also

* [/next](/macroquest/commands/macro-commands/next.md)
* [/while](/macroquest/commands/macro-commands/while.md)
* [/break](/macroquest/commands/macro-commands/break.md)
* [/continue](/macroquest/commands/macro-commands/continue.md)
* [Macro\_Reference](/macroquest/documentation/macro-reference.md)
* [Slash Commands](/macroquest/commands/slash-commands.md)


---

# 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://macroquest.gitbook.io/macroquest/commands/macro-commands/for.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.
