---
name: "for"
type: command
status: ok
groups: ["Program Flow"]
arma3_since: "0.50"
locality: {"argument": null, "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/for
revid: 353544
updated: 2024-03-19T12:01:33Z
---

# for

*Command · Program Flow · Arma 3 v0.50 · introduced in Armed Assault v1.00*

## Description

This operator creates a [For Type](../topics/For_Type.md) which is used in the for-constructs.

> [!WARNING]
> The alternative syntax `for _array do _code` is [slower](../topics/Code_Optimisation.md#for) than the first syntax because the condition is checked in every iteration, and it has a few bugs.
>
> It is only kept for backward compatibility; always use the first syntax `for "_i" from 0 to _end do _code`.

## Syntax

### Syntax 1

```sqf
for arguments
```

**Parameters:**

- `arguments`: [String](../topics/String.md) - declares the index variable name like "_myVar". [from](../commands/from.md) and [to](../commands/to.md) are required, [step](../commands/step.md) is optional.

**Returns:** [For Type](../topics/For_Type.md)

### Syntax 2

```sqf
for [init, condition, codeToExecute]
```

**Parameters:**

- `init`: [Code](../topics/Code.md) - loop variable definition
- `condition`: [Code](../topics/Code.md) - [call](../commands/call.md)ed code returns [false](../commands/false.md), leave the [for](../commands/for.md)-loop
- `codeToExecute`: [Code](../topics/Code.md) - code to be run on each loop

**Returns:** [For Type](../topics/For_Type.md)

## Examples

### Example 1

```sqf
// will output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (the to value being inclusive)
for "_i" from 1 to 10 do { systemChat str _i; };
```

### Example 2

```sqf
// will output 9, 7, 5, 3, 1
for "_i" from 9 to 1 step -2 do { systemChat str _i; };
```

### Example 3

```sqf
// will output 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
for [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] do { systemChat str _i };
```

### Example 4

```sqf
// BAD CODE
_i = 100;
for [{ _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ };
hint str _i; // 5

// GOOD CODE (private keyword is recommended)
_i = 100;
for [{ private _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ };
hint str _i; // 100

// BEST CODE (primary syntax, fastest)
_i = 100;
for "_i" from 0 to 4 do { /* code */ };
hint str _i; // 100
```

## Problems

> [!NOTE]
> **Arma 2:** There is an Arma 2 issue (not present in Arma 2:OA nor in Arma 3) where having a capital letter in the variable name **will** throw an error:
>
> ```sqf
> for "_i" from 0 to 2 do { hintSilent str _i; }; // works
> for "_I" from 0 to 2 do { hintSilent str _I; }; // "variable _i is not defined" error
> ```

## See also

[Control Structures](../topics/Control_Structures.md) · [while](../commands/while.md) · [do](../commands/do.md)

## Community notes

**Alef** — 2010-04-29

```sqf
for "_i" from 0 to  0 do {}; // will do once, with _i = 0
for "_i" from 0 to -1 do {}; // will not do
```

---

**ffur2007slx2_5** — 2014-04-15

- Arma 3 v1.16 Please note the difference between the two syntaxes; `for *arguments*` detects Boolean in each scope while `for *array*` doesn’t. e.g.

| command | Structure | Summary |
|---|---|---|
| [for](../commands/for.md) *array* | ```sqf<br>a = 0; b = true;<br>for [{ _i = 0 }, { _i < 10 && b }, { _i = _i + 1 }] do<br>{<br>    a = a + 1;<br>    if (a >= 7) then {b = false}<br>};<br>``` | loop can be exited via [Boolean](../topics/Boolean.md) control, possible workaround can be e.g [BIS_fnc_areEqual](../functions/BIS_fnc_areEqual.md) |
| [for](../commands/for.md) *arguments* | ```sqf<br>a = 0;<br>for "_i" from 0 to 10 do<br>{<br>    a = a + 1;<br>    if (a >= 7) exitwith {};<br>};<br>``` | has to be exited via [exitWith](../commands/exitWith.md) |

- Never try to tell a decimal number via binary number in a loop; otherwise the loop will be infinite:

```sqf
for [{_a = 0},{_a != 1},{_a = _a + 0.1}] do {}; // an infinite loop; _a will never be 1 so the scope will always be true
```

Any binary number behind the decimal point is always the sum of 1/2, 1/4, 1/8, 1/16 etc. so decimal number with odd denominator like 1/3 or 1/10 cannot be exactly equal to each other.
- Avoid too large factorial multiply which may loose the leading indicator in result. And 12 is the biggest accessable factor in this example.

```sqf
for [{_a = 2; _b = 1;},{_a < 100},{_a = _a + 1}] do {_b = _b * _a}; // _b =  1.#INF
```

---

**Killzone_Kid** — 2015-06-04

Variable name doesn't have to start with _. could be:

```sqf
for "LAlala" from 0 to 0 do { hint str LAlala }; // 0
```

The variable *LAlala* will exist only inside [do](../commands/do.md) {} scope and will not overwrite any variable of the same name that existed before.

---

**Nickorr** — 2015-06-04

Dont use this notation if you plan to change the cycle ranges dynamically. The range values are checked only before the cycle started. Use `for *array*` instead.

Example, that won't work correctly:

```sqf
_xy = [1,2,3,4,5,6,7,8,9,10];
for "_i" from 0 to (count _xy - 1) do
{
    if ( _xy select _i == 3 ) then
    {
        _xy deleteAt _i;
        _i = _i - 1;
    };
};
```

(Here the  step will still be checked by the cycle, which will lead to "out of the array range" error.)

This code will work correctly:

```sqf
_xy = [1,2,3,4,5,6,7,8,9,10];
for [{_i = 1},{_i<=(count _xy - 1)},{_i=_i+1}] do
{
    if ( _xy select _i == 3 ) then
    {
        _xy deleteAt _i;
        _i = _i - 1;
    };
};
```

(The last step here will be `_i = 8` with array looking like this: `[1,2,4,5,6,7,8,9,10]`)

---

**DreadedEntity** — 2016-01-29

For loops can be safely nested. This means that there should not be any problems with recursion.

```sqf
_array = [];
for "_i" from 0 to 3 do
{
    for "_i" from 0 to 3 do
    {
        _array pushBack _i;
    };
    _array pushBack _i;
};
hint str _array;
```

<sub>Source: [BI Community Wiki — for](https://community.bistudio.com/wiki/for) (revision 353544).</sub>
