---
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 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](../topics/Identifier.md) = [expression](../topics/Expression.md)

**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.

**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 [Number](../topics/Number.md)s. Arithmetic operations always return a [Number](../topics/Number.md).

|  |  |  |  |
|---|---|---|---|
| + Unary arithmetic operators (in order of precedence) | Operator | Name | Example |
| - | Negation | ```sqf<br>-a<br>``` |  |
| + | Duplication | ```sqf<br>+a<br>``` |  |
| ( | Bracket | ```sqf<br>(expression)<br>``` |  |

|  |  |  |  |
|---|---|---|---|
| + Binary arithmetic operators | Operator | Name | Example |
| + | Addition | ```sqf<br>a + b<br>``` |  |
| - | Subtraction | ```sqf<br>a - b<br>``` |  |
| * | Multiplication | ```sqf<br>a * b<br>``` |  |
| / | Division | ```sqf<br>a / b<br>``` |  |
| % | Modulo | ```sqf<br>a % b<br>``` |  |
| mod | Modulo | ```sqf<br>a mod b<br>``` |  |
| ^ | Raise to the power of | ```sqf<br>a ^ b<br>``` |  |

> [!NOTE]
> See also Math Commands.

#### Logical Operators

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

|  |  |  |  |
|---|---|---|---|
| + Unary logical operators | Operator | Name | Example |
| ! | Not | ```sqf<br>!a<br>``` |  |
| not | Not | ```sqf<br>not a<br>``` |  |

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 | ```sqf<br>a && b<br>``` |  |
| and | And | ```sqf<br>a and b<br>``` |  |
| \|\| | Or | ```sqf<br>a \|\| b<br>``` |  |
| or | Or | ```sqf<br>a or b<br>``` |  |

- `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 | ```sqf<br>((a \|\| b) && !(a && b))<br>``` |  |
| Nor | ```sqf<br>!(a \|\| b)<br>``` |  |
| Nand | ```sqf<br>!(a && b)<br>``` |  |

- `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 | ```sqf<br>a == b<br>``` |  |
| != | Not equal | ```sqf<br>a != b<br>``` |  |
| < | Less than | ```sqf<br>a < b<br>``` |  |
| > | Greater than | ```sqf<br>a > b<br>``` |  |
| <= | Less or equal | ```sqf<br>a <= b<br>``` |  |
| >= | Greater or equal | ```sqf<br>a >= b<br>``` |  |

> [!NOTE]
>

#### 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 | ```sqf<br>+myArray<br>``` |  |

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 | ```sqf<br>myArray1 + myArray2<br>``` |  |
| - | Removal | ```sqf<br>myArray1 - myArray2<br>``` |  |

- `+` 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 [String](../topics/String.md)s.
The return value of a string operation is a [String](../topics/String.md).

|  |  |  |  |
|---|---|---|---|
| + Binary string operators | Operator | Name | Example |
| + | Concatenation | ```sqf<br>myString1 + myString2<br>``` |  |

- `+` 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 | ```cpp<br>myProperty[] += {element_1, element_2};<br>``` |  |

**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

See [Order of Precedence](../topics/Order_of_Precedence.md).

### See Also

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

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