---
name: "Operators"
type: topic
source: https://community.bistudio.com/wiki/Operators
revid: 363637
updated: 2024-04-09T03:49:39Z
---

# Operators

**Operators** are the base commands each programming language is built on. They provide ability to perform basic mathematical and logical operations.

### Requirements

To understand this article, you should read the following articles:

- [Variables](../topics/Variables.md)

### Terms

**Operand:** An [operand](../topics/Operand.md) is any value given to an operator.

**Expression:** An expression is basically any code that returns a value. Read [expression](../topics/Expression.md) for more information.

**Unary Operator:** An unary operator is an operator that requires only one operand.

Unary operation:

```
operator expression
```

**Binary Operator:** A binary operator is an operator that requires two operands.

Binary operation:

```
expression operator expression
```

### Operators

#### Assignment Operators

Assignment operators are used to assign values to a [variable](../topics/Variables.md). OFP's scripting language provides only one assignment operator.

**Assignment:**

```
identifier = expression
```

**Example 1:**

```sqf
a = b
```

You might think that this operator compares `a` and `b`, but that is not the case. `=` simply sets the left value to be the right one. Other assignment operators like `+=`, `-=` that can be found in other programming languages do not exist in [SQF](../topics/SQF_Syntax.md)/[SQS](../topics/SQS_Syntax.md).

**Example 2:**

```sqf
a = b * c
```

#### Arithmetic Operators

Remember arithmetic operations from school? These work just the same way.
All operands of arithmetic operations must be [Numbers](../topics/Number.md). Arithmetic operations always return a [Number](../topics/Number.md).

**Unary arithmetic operators (in order of precedence)**

| Operator | Name | Example |
|---|---|---|
| - | Negation | `-a` |
| + | Duplication | `+a` |
| ( | Bracket | `(expression)` |

**Binary arithmetic operators**

| Operator | Name | Example |
|---|---|---|
| + | Addition | `a + b` |
| - | Subtraction | `a - b` |
| * | Multiplication | `a * b` |
| / | Division | `a / b` |
| % | Modulo | `a % b` |
| mod | Modulo | `a mod b` |
| ^ | Raise to the power of | `a ^ b` |

> [!NOTE]
> See also Math Commands.

#### Logical Operators

Logical operators evaluate [Boolean](../topics/Boolean.md) values. All operands of logical operations are [Booleans](../topics/Boolean.md).
A logical operation always returns a [Boolean](../topics/Boolean.md).

**Unary logical operators**

| Operator | Name | Example |
|---|---|---|
| ! | Not | `!a` |
| not | Not | `not a` |

The `Not`-operator always returns the inverse [Boolean](../topics/Boolean.md) value. If a [Boolean](../topics/Boolean.md) `a` is `true`, `!a` returns `false` and vice versa.

**Binary logical operators**

| Operator | Name | Example |
|---|---|---|
| && | And | `a && b` |
| and | And | `a and b` |
| \|\| | Or | `a \|\| b` |
| or | Or | `a or b` |

- `And` only returns `true` if *both* operands are `true`
- `Or` returns `true` if *one or both* operands are `true`

There is no Xor, Nor and Nand operator. Those can be simulated using the basic operators though:

**Combined logical operators**

| Name | Combination |
|---|---|
| Xor | `((a \|\| b) && !(a && b))` |
| Nor | `!(a \|\| b)` |
| Nand | `!(a && b)` |

- `Xor` returns `true` if *exactly one* of both values is `true`
- `Nor` returns `true` if *none* of both values is `true`
- `Nand` returns `true` if *not both* values are `true` at the same time

#### Comparison Operators

Comparison operators compare two values. Operands of comparisons may be of type [Number](../topics/Number.md), [Side](../topics/Side.md), [String](../topics/String.md), [Object](../topics/Object.md), [Group](../topics/Group.md), [Structured Text](../topics/Structured_Text.md), [Config](../topics/Config.md), [Display](../topics/Display.md) or [Control](../topics/Control.md) for `==` and `!=`, and [Number](../topics/Number.md) for `< > >= <=`.
Comparisons always return a [Boolean](../topics/Boolean.md): `true` if the comparison matches, `false` if not.

**Comparison operators**

| Operator | Name | Example |
|---|---|---|
| == | Equal | `a == b` |
| != | Not equal | `a != b` |
| < | Less than | `a < b` |
| > | Greater than | `a > b` |
| <= | Less or equal | `a <= b` |
| >= | Greater or equal | `a >= b` |

> [!NOTE]
> | invalid before Arma 3 v2.00 | valid in all versions |
> |---|---|
> | `a == true` | `a` |
> | `a == false` | `!a` |
>
> You could not compare [Boolean](../topics/Boolean.md) values with [==](../commands/a_~3D~3D_b.md) before **Arma 3 v2.00** (in Arma 3 before that, use [isEqualTo](../commands/isEqualTo.md)).\
> Comparing a [Boolean](../topics/Boolean.md) value with `true` is the same as the value itself.\
> Comparing a [Boolean](../topics/Boolean.md) value with `false` is the same as the *inverse* value.

#### Array Operators

The scripting language offers own operators to deal with arrays. All operands, of course, have to be of type [Array](../topics/Array.md).
The return value of an array operation is an [Array](../topics/Array.md).

**Unary array operators**

| Operator | Name | Example |
|---|---|---|
| + | Copy | `+myArray` |

Normally arrays are assigned *by reference*. That means, if you assign array `a` to array `b` and change `a` afterwards, also `b` is changed. Use the copy operator to avoid this otherwise useful feature.

**Example 1:**

```sqf
_arrayA = [1,2];
_arrayB = _arrayA;
_arrayA set [0,5];

// _arrayA => [5,2]
// _arrayB => [5,2]
```

**Example 2:**

```sqf
_arrayA = [1,2];
_arrayB = +_arrayA;
_arrayA set [0,5];

// _arrayA => [5,2]
// _arrayB => [1,2]
```

**Binary array operators**

| Operator | Name | Example |
|---|---|---|
| + | Concatenation | `myArray1 + myArray2` |
| - | Removal | `myArray1 - myArray2` |

- `+` adds the second operand on the end of the first operand
- `-` removes all elements of the second operand from the first operand

**Example 1:**

```sqf
_arrayA = [1,2];
_arrayB = [3,2,4];

_arrayC = _arrayA + _arrayB;

// _arrayC => [1,2,3,2,4]
```

**Example 2:**

```sqf
_arrayA = [1,2,3,2,4];
_arrayB = [2,3];

_arrayC = _arrayA - _arrayB;

// _arrayC => [1,4]
```

#### String Operators

The scripting language offers one single string operator to concatenate strings. Both operands must be [Strings](../topics/String.md).
The return value of a string operation is a [String](../topics/String.md).

**Binary string operators**

| Operator | Name | Example |
|---|---|---|
| + | Concatenation | `myString1 + myString2` |

- `+` adds the second operand on the end of the first operand

**Example:**

```sqf
_stringA = "Hello ";
_stringB = "World!";

_stringC = _stringA + _stringB;

// _stringC => "Hello World!"
```

#### Config Operators

**Config operators**

| Operator | Name | Example |
|---|---|---|
| += | Append Array | `myProperty[] += {element_1, element_2};` |

**Example:**

```cpp
class B_Soldier_F;
class MySoldier: B_Soldier_F
{
    RespawnItems[] += {"FirstAidKit"}; //Adds additional FAK
};

/*
    Original RespawnItems[] array in config viewer:
    RespawnItems[] = {"FirstAidKit"};

    Modified RespawnItems[] array in config viewer:
    RespawnItems[] = {"FirstAidKit","FirstAidKit"};
*/
```

### Order of Precedence

Order of operations, also called operator precedence, is a set of rules specifying which procedures should be performed first in a mathematical expression.

#### Precedence Overview

> [!NOTE]
> - Highest precedence means highest priority
> - Associativity is (then) done from left to right, for example `3 + 5 + 8` will be processed as `((3 + 5) + 8)`

| Precedence | Type of Operator | Examples |
|---|---|---|
| 11 | Nular operators (commands with no arguments):<br>- `commandName` | - [Variables](../topics/Variables.md)<br>- Values<br>- `()` / `[]` / `{}` / `""` / `''` -enclosed expressions |
| 10 | Unary operators (commands with 1 argument):<br>- `commandName` a | - [+a](../commands/~2B.md)<br>- [-a](../commands/-.md)<br>- [+array](../commands/~2B.md)<br>- [! b](../commands/~21_a.md)<br>- [not b](../commands/not.md) |
| 9 | Hash-select operator | - [array # index](../commands/a_hash_b.md) |
| 8 | Power operator | - [a ^ b](../commands/a_~5E_b.md) |
| 7 | - [Number](../topics/Number.md) multiplication, division and remainder<br>- [atan2](../commands/atan2.md)<br>- [Config](../topics/Config.md) access ([/](../commands/a_~2F_b.md)) | - [a * b](../commands/a_~2A_b.md)<br>- [a / b](../commands/a_~2F_b.md)<br>- [a % b](../commands/a_~25_b.md)<br>- [a mod b](../commands/mod.md)<br>- [a / b](../commands/a_~2F_b.md)<br>- [a atan2 b](../commands/atan2.md) |
| 6 | - [Number](../topics/Number.md) addition and subtraction<br>- [Array](../topics/Array.md) addition and subtraction<br>- [String](../topics/String.md) addition<br>- [min](../commands/min.md) and [max](../commands/max.md) commands | - [a + b](../commands/~2B.md)<br>- [a - b](../commands/-.md)<br>- [arr1 + arr2](../commands/~2B.md)<br>- [arr1 - arr2](../commands/-.md)<br>- [str1 + str2](../commands/~2B.md)<br>- [a min b](../commands/min.md)<br>- [a max b](../commands/max.md) |
| 5 | - [else](../commands/else.md) ([if](../commands/if.md)-[then](../commands/then.md)-[else](../commands/else.md) structure) | N/A |
| 4 | Binary operators (commands with 2 arguments):<br>- a `commandName` b | - [setDir](../commands/setDir.md)<br>- [switch colon (:)](../commands/a_~3A_b.md) |
| 3 | - [==](../commands/a_~3D~3D_b.md) and [!=](../commands/a_~21~3D_b.md) comparisons<br>- [Number](../topics/Number.md) comparisons<br>- [Config](../topics/Config.md) access ([>>](../commands/config_greater_greater_name.md)) | - [a == b](../commands/a_~3D~3D_b.md)<br>- [a != b](../commands/a_~21~3D_b.md)<br>- [a > b](../commands/a_greater_b.md)<br>- [a < b](../commands/a_less_b.md)<br>- [a >= b](../commands/a_greater~3D_b.md)<br>- [a <= b](../commands/a_less~3D_b.md)<br>- [config >> name](../commands/config_greater_greater_name.md) |
| 2 | Logical [and](../commands/and.md) operator | - [&&](../commands/a_~26~26_b.md)<br>- [and](../commands/and.md) |
| 1 | Logical [or](../commands/or.md) operator | - [\|\|](../commands/a_or_b.md)<br>- [or](../commands/or.md) |

#### Examples

| Input | Process | Comment |
|---|---|---|
| `1 + 2 * 3` | `1 + (2 * 3)` | result equals 7, and not 9 (see also [PEMDAS](https://en.wikipedia.org/wiki/Order_of_operations)) |
| `sleep 10 + random 20` | `(sleep 10) + random 20` | `sleep 10` will return [Nothing](../topics/Nothing.md), then `+ random 20` will be calculated but not used.<br>`sleep (10 + random 20)` should be used instead |

### See Also

- [SQF Syntax](../topics/SQF_Syntax.md)
- [SQS Syntax](../topics/SQS_Syntax.md)

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