---
name: "switch"
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/switch
revid: 352209
updated: 2024-02-01T23:26:04Z
---

# switch

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

## Description

Checks if the given parameter matches any [case](../commands/case.md). If so, the code block of that case will be executed. After that the switch ends so no further cases will be checked.

If a case has no code block the code of the next case will automatically be executed, making it possible to formulate a logical "or" for cases which would otherwise contain the exact same code (see [#Example 2](../commands/switch.md#example-2)).

The [default](../commands/default.md) block will be executed **only** if no case matches, no matter its position inside the code block. It is not a [case](../commands/case.md), and will **never** be entered by fallthrough.

> [!NOTE]
> - [String](../topics/String.md) comparison is case-sensitive. Use [toUpper](../commands/toUpper.md), [toLower](../commands/toLower.md), [toLowerANSI](../commands/toLowerANSI.md) or [toUpperANSI](../commands/toUpperANSI.md) to force all [strings](../topics/String.md) to the same case.
> - if no [default](../commands/default.md) block is provided and no [case](../commands/case.md) is matched, the switch block returns the default value [true](../commands/true.md), otherwise it returns what the valid case block returns.

## Syntax

```sqf
switch value
```

**Parameters:**

- `value`: [Anything](../topics/Anything.md)

**Returns:** [Switch Type](../topics/Switch_Type.md)

## Examples

### Example 1

```sqf
switch (floor random 5) do
{
    case 1: { hint "one"; };
    case 2: { hint "two"; };
    default { hint "zero, three or four" };
};
```

### Example 2

```sqf
switch (_condition) do
{
    case "string1";
    case "string2": { hint "string1 or string2" };
    case "string3";
    case "string4": { hint "string3 or string4" };
    case "string5"; // will never enter default's code
    default { hint "default" };
};
```

### Example 3

```sqf
private _color = switch (side player) do
{
    case west: { "ColorGreen" };
    case east: { "ColorRed" };
};
```

### Example 4

```sqf
private _fn_moveForward =    { /*...code...*/ };
private _fn_moveBackward =    { /*...code...*/ };
private _fn_invalidKey =    { /*...code...*/ };

switch true do
{
    case (_dikCode in actionKeys "MoveForward"):    _fn_moveForward;
    case (_dikCode in actionKeys "MoveBackward"):    _fn_moveBackward;
    default _fn_invalidKey;
};
```

### Example 5

```sqf
switch _var do
{
    case "0";
    default { hint str ["default", _var] };
    case "3": { hint str ["3", _var] };
    case "1";
    case "4";
    case "2": { hint str ["2", _var] };
};

_var = "0"; // -> ["3", "0"]
_var = "1"; // -> ["2", "1"]
_var = "2"; // -> ["2", "2"]
_var = "3"; // -> ["3", "3"]
_var = "4"; // -> ["2", "4"]
_var = "5"; // -> ["default", "5"]
```

## See also

[Control Structures](../topics/Control_Structures.md) · a:b · [case](../commands/case.md) · [default](../commands/default.md)

## Community notes

**ColonelSandersLite** — 2007-08-07

Be careful of the parentheses around the variable you're switching on.
If you accidentally use braces instead (e.g: `switch { _myVar } do { /*... */ }`), it won't error, but will always return default.

> [!NOTE]
> This is due to `{ _myvar }` being [Code](../topics/Code.md).

---

**Iva** — 2009-10-06

It's possible to use [Boolean](../topics/Boolean.md) value as a switch and [Code](../topics/Code.md) as case. One thing to take special care in such case is that code must be in parentheses. Example:

```sqf
switch (true) do
{
    case (_boolVar): { someCode };
    case (unit1 distance unit2 > 5): { someCode };
};
```

---

**Freddo3000** — 2020-11-01

You are able to call code within the switch statements, such as:

```sqf
private _myFunc = {
    systemChat "0";
    case 1: { systemChat "1" };
    systemChat "2";
};

private _myParameter = 1;

switch (_myParameter) do
{
    _myParameter call _myFunc;
    case 1: { systemChat "4" };
    systemChat "3";
    default { systemChat "-1"; };
};
```

However the order in which it is executed is difficult to make sense of, see the code comment.

This is used by [BIS_fnc_missionConversations](../functions/BIS_fnc_missionConversations.md) and [BIS_fnc_missionTasks](../functions/BIS_fnc_missionTasks.md)

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