{"name":"Operators","title":"Operators","type":"topic","url":"https://community.bistudio.com/wiki/Operators","revid":363637,"updated":"2024-04-09T03:49:39Z","aliases":[],"markdown":"**Operators** are the base commands each programming language is built on. They provide ability to perform basic mathematical and logical operations.\n\n### Requirements\n\nTo understand this article, you should read the following articles:\n\n- Variables\n\n### Terms\n\n**Operand:** An operand is any value given to an operator.\n\n**Expression:** An expression is basically any code that returns a value. Read expression for more information.\n\n**Unary Operator:** An unary operator is an operator that requires only one operand.\n\nUnary operation:\n\n```\noperator expression\n```\n\n**Binary Operator:** A binary operator is an operator that requires two operands.\n\nBinary operation:\n\n```\nexpression operator expression\n```\n\n### Operators\n\n#### Assignment Operators\n\nAssignment operators are used to assign values to a variable. OFP's scripting language provides only one assignment operator.\n\n**Assignment:**\n\n```\nidentifier = expression\n```\n\n**Example 1:**\n\n```sqf\na = b\n```\n\nYou 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/SQS.\n\n**Example 2:**\n\n```sqf\na = b * c\n```\n\n#### Arithmetic Operators\n\nRemember arithmetic operations from school? These work just the same way.\nAll operands of arithmetic operations must be Numbers. Arithmetic operations always return a Number.\n\n**Unary arithmetic operators (in order of precedence)**\n\n| Operator | Name | Example |\n|---|---|---|\n| - | Negation | `-a` |\n| + | Duplication | `+a` |\n| ( | Bracket | `(expression)` |\n\n**Binary arithmetic operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| + | Addition | `a + b` |\n| - | Subtraction | `a - b` |\n| * | Multiplication | `a * b` |\n| / | Division | `a / b` |\n| % | Modulo | `a % b` |\n| mod | Modulo | `a mod b` |\n| ^ | Raise to the power of | `a ^ b` |\n\n> [!NOTE]\n> See also Math Commands.\n\n#### Logical Operators\n\nLogical operators evaluate Boolean values. All operands of logical operations are Booleans.\nA logical operation always returns a Boolean.\n\n**Unary logical operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| ! | Not | `!a` |\n| not | Not | `not a` |\n\nThe `Not`-operator always returns the inverse Boolean value. If a Boolean `a` is `true`, `!a` returns `false` and vice versa.\n\n**Binary logical operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| && | And | `a && b` |\n| and | And | `a and b` |\n| \\|\\| | Or | `a \\|\\| b` |\n| or | Or | `a or b` |\n\n- `And` only returns `true` if *both* operands are `true`\n- `Or` returns `true` if *one or both* operands are `true`\n\nThere is no Xor, Nor and Nand operator. Those can be simulated using the basic operators though:\n\n**Combined logical operators**\n\n| Name | Combination |\n|---|---|\n| Xor | `((a \\|\\| b) && !(a && b))` |\n| Nor | `!(a \\|\\| b)` |\n| Nand | `!(a && b)` |\n\n- `Xor` returns `true` if *exactly one* of both values is `true`\n- `Nor` returns `true` if *none* of both values is `true`\n- `Nand` returns `true` if *not both* values are `true` at the same time\n\n#### Comparison Operators\n\nComparison operators compare two values. Operands of comparisons may be of type Number, Side, String, Object, Group, Structured Text, Config, Display or Control for `==` and `!=`, and Number for `< > >= <=`.\nComparisons always return a Boolean: `true` if the comparison matches, `false` if not.\n\n**Comparison operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| == | Equal | `a == b` |\n| != | Not equal | `a != b` |\n| < | Less than | `a < b` |\n| > | Greater than | `a > b` |\n| <= | Less or equal | `a <= b` |\n| >= | Greater or equal | `a >= b` |\n\n> [!NOTE]\n> | invalid before Arma 3 v2.00 | valid in all versions |\n> |---|---|\n> | `a == true` | `a` |\n> | `a == false` | `!a` |\n>\n> You could not compare Boolean values with == before **Arma 3 v2.00** (in Arma 3 before that, use isEqualTo).\n> Comparing a Boolean value with `true` is the same as the value itself.\n> Comparing a Boolean value with `false` is the same as the *inverse* value.\n\n#### Array Operators\n\nThe scripting language offers own operators to deal with arrays. All operands, of course, have to be of type Array.\nThe return value of an array operation is an Array.\n\n**Unary array operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| + | Copy | `+myArray` |\n\nNormally 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.\n\n**Example 1:**\n\n```sqf\n_arrayA = [1,2];\n_arrayB = _arrayA;\n_arrayA set [0,5];\n\n// _arrayA => [5,2]\n// _arrayB => [5,2]\n```\n\n**Example 2:**\n\n```sqf\n_arrayA = [1,2];\n_arrayB = +_arrayA;\n_arrayA set [0,5];\n\n// _arrayA => [5,2]\n// _arrayB => [1,2]\n```\n\n**Binary array operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| + | Concatenation | `myArray1 + myArray2` |\n| - | Removal | `myArray1 - myArray2` |\n\n- `+` adds the second operand on the end of the first operand\n- `-` removes all elements of the second operand from the first operand\n\n**Example 1:**\n\n```sqf\n_arrayA = [1,2];\n_arrayB = [3,2,4];\n\n_arrayC = _arrayA + _arrayB;\n\n// _arrayC => [1,2,3,2,4]\n```\n\n**Example 2:**\n\n```sqf\n_arrayA = [1,2,3,2,4];\n_arrayB = [2,3];\n\n_arrayC = _arrayA - _arrayB;\n\n// _arrayC => [1,4]\n```\n\n#### String Operators\n\nThe scripting language offers one single string operator to concatenate strings. Both operands must be Strings.\nThe return value of a string operation is a String.\n\n**Binary string operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| + | Concatenation | `myString1 + myString2` |\n\n- `+` adds the second operand on the end of the first operand\n\n**Example:**\n\n```sqf\n_stringA = \"Hello \";\n_stringB = \"World!\";\n\n_stringC = _stringA + _stringB;\n\n// _stringC => \"Hello World!\"\n```\n\n#### Config Operators\n\n**Config operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| += | Append Array | `myProperty[] += {element_1, element_2};` |\n\n**Example:**\n\n```cpp\nclass B_Soldier_F;\nclass MySoldier: B_Soldier_F\n{\n    RespawnItems[] += {\"FirstAidKit\"}; //Adds additional FAK\n};\n\n/*\n    Original RespawnItems[] array in config viewer:\n    RespawnItems[] = {\"FirstAidKit\"};\n\n    Modified RespawnItems[] array in config viewer:\n    RespawnItems[] = {\"FirstAidKit\",\"FirstAidKit\"};\n*/\n```\n\n### Order of Precedence\n\nOrder of operations, also called operator precedence, is a set of rules specifying which procedures should be performed first in a mathematical expression.\n\n#### Precedence Overview\n\n> [!NOTE]\n> - Highest precedence means highest priority\n> - Associativity is (then) done from left to right, for example `3 + 5 + 8` will be processed as `((3 + 5) + 8)`\n\n| Precedence | Type of Operator | Examples |\n|---|---|---|\n| 11 | Nular operators (commands with no arguments):<br>- `commandName` | - Variables<br>- Values<br>- `()` / `[]` / `{}` / `\"\"` / `''` -enclosed expressions |\n| 10 | Unary operators (commands with 1 argument):<br>- `commandName` a | - +a<br>- -a<br>- +array<br>- ! b<br>- not b |\n| 9 | Hash-select operator | - array # index |\n| 8 | Power operator | - a ^ b |\n| 7 | - Number multiplication, division and remainder<br>- atan2<br>- Config access (/) | - a * b<br>- a / b<br>- a % b<br>- a mod b<br>- a / b<br>- a atan2 b |\n| 6 | - Number addition and subtraction<br>- Array addition and subtraction<br>- String addition<br>- min and max commands | - a + b<br>- a - b<br>- arr1 + arr2<br>- arr1 - arr2<br>- str1 + str2<br>- a min b<br>- a max b |\n| 5 | - else (if-then-else structure) | N/A |\n| 4 | Binary operators (commands with 2 arguments):<br>- a `commandName` b | - setDir<br>- switch colon (:) |\n| 3 | - == and != comparisons<br>- Number comparisons<br>- Config access (>>) | - a == b<br>- a != b<br>- a > b<br>- a < b<br>- a >= b<br>- a <= b<br>- config >> name |\n| 2 | Logical and operator | - &&<br>- and |\n| 1 | Logical or operator | - \\|\\|<br>- or |\n\n#### Examples\n\n| Input | Process | Comment |\n|---|---|---|\n| `1 + 2 * 3` | `1 + (2 * 3)` | result equals 7, and not 9 (see also [PEMDAS](https://en.wikipedia.org/wiki/Order_of_operations)) |\n| `sleep 10 + random 20` | `(sleep 10) + random 20` | `sleep 10` will return Nothing, then `+ random 20` will be calculated but not used.<br>`sleep (10 + random 20)` should be used instead |\n\n### See Also\n\n- SQF Syntax\n- SQS Syntax","sections":[{"heading":null,"level":1,"markdown":"**Operators** are the base commands each programming language is built on. They provide ability to perform basic mathematical and logical operations."},{"heading":"Requirements","level":3,"markdown":"To understand this article, you should read the following articles:\n\n- Variables"},{"heading":"Terms","level":3,"markdown":"**Operand:** An operand is any value given to an operator.\n\n**Expression:** An expression is basically any code that returns a value. Read expression for more information.\n\n**Unary Operator:** An unary operator is an operator that requires only one operand.\n\nUnary operation:\n\n```\noperator expression\n```\n\n**Binary Operator:** A binary operator is an operator that requires two operands.\n\nBinary operation:\n\n```\nexpression operator expression\n```"},{"heading":"Operators","level":3,"markdown":""},{"heading":"Assignment Operators","level":4,"markdown":"Assignment operators are used to assign values to a variable. OFP's scripting language provides only one assignment operator.\n\n**Assignment:**\n\n```\nidentifier = expression\n```\n\n**Example 1:**\n\n```sqf\na = b\n```\n\nYou 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/SQS.\n\n**Example 2:**\n\n```sqf\na = b * c\n```"},{"heading":"Arithmetic Operators","level":4,"markdown":"Remember arithmetic operations from school? These work just the same way.\nAll operands of arithmetic operations must be Numbers. Arithmetic operations always return a Number.\n\n**Unary arithmetic operators (in order of precedence)**\n\n| Operator | Name | Example |\n|---|---|---|\n| - | Negation | `-a` |\n| + | Duplication | `+a` |\n| ( | Bracket | `(expression)` |\n\n**Binary arithmetic operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| + | Addition | `a + b` |\n| - | Subtraction | `a - b` |\n| * | Multiplication | `a * b` |\n| / | Division | `a / b` |\n| % | Modulo | `a % b` |\n| mod | Modulo | `a mod b` |\n| ^ | Raise to the power of | `a ^ b` |\n\n> [!NOTE]\n> See also Math Commands."},{"heading":"Logical Operators","level":4,"markdown":"Logical operators evaluate Boolean values. All operands of logical operations are Booleans.\nA logical operation always returns a Boolean.\n\n**Unary logical operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| ! | Not | `!a` |\n| not | Not | `not a` |\n\nThe `Not`-operator always returns the inverse Boolean value. If a Boolean `a` is `true`, `!a` returns `false` and vice versa.\n\n**Binary logical operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| && | And | `a && b` |\n| and | And | `a and b` |\n| \\|\\| | Or | `a \\|\\| b` |\n| or | Or | `a or b` |\n\n- `And` only returns `true` if *both* operands are `true`\n- `Or` returns `true` if *one or both* operands are `true`\n\nThere is no Xor, Nor and Nand operator. Those can be simulated using the basic operators though:\n\n**Combined logical operators**\n\n| Name | Combination |\n|---|---|\n| Xor | `((a \\|\\| b) && !(a && b))` |\n| Nor | `!(a \\|\\| b)` |\n| Nand | `!(a && b)` |\n\n- `Xor` returns `true` if *exactly one* of both values is `true`\n- `Nor` returns `true` if *none* of both values is `true`\n- `Nand` returns `true` if *not both* values are `true` at the same time"},{"heading":"Comparison Operators","level":4,"markdown":"Comparison operators compare two values. Operands of comparisons may be of type Number, Side, String, Object, Group, Structured Text, Config, Display or Control for `==` and `!=`, and Number for `< > >= <=`.\nComparisons always return a Boolean: `true` if the comparison matches, `false` if not.\n\n**Comparison operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| == | Equal | `a == b` |\n| != | Not equal | `a != b` |\n| < | Less than | `a < b` |\n| > | Greater than | `a > b` |\n| <= | Less or equal | `a <= b` |\n| >= | Greater or equal | `a >= b` |\n\n> [!NOTE]\n> | invalid before Arma 3 v2.00 | valid in all versions |\n> |---|---|\n> | `a == true` | `a` |\n> | `a == false` | `!a` |\n>\n> You could not compare Boolean values with == before **Arma 3 v2.00** (in Arma 3 before that, use isEqualTo).\n> Comparing a Boolean value with `true` is the same as the value itself.\n> Comparing a Boolean value with `false` is the same as the *inverse* value."},{"heading":"Array Operators","level":4,"markdown":"The scripting language offers own operators to deal with arrays. All operands, of course, have to be of type Array.\nThe return value of an array operation is an Array.\n\n**Unary array operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| + | Copy | `+myArray` |\n\nNormally 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.\n\n**Example 1:**\n\n```sqf\n_arrayA = [1,2];\n_arrayB = _arrayA;\n_arrayA set [0,5];\n\n// _arrayA => [5,2]\n// _arrayB => [5,2]\n```\n\n**Example 2:**\n\n```sqf\n_arrayA = [1,2];\n_arrayB = +_arrayA;\n_arrayA set [0,5];\n\n// _arrayA => [5,2]\n// _arrayB => [1,2]\n```\n\n**Binary array operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| + | Concatenation | `myArray1 + myArray2` |\n| - | Removal | `myArray1 - myArray2` |\n\n- `+` adds the second operand on the end of the first operand\n- `-` removes all elements of the second operand from the first operand\n\n**Example 1:**\n\n```sqf\n_arrayA = [1,2];\n_arrayB = [3,2,4];\n\n_arrayC = _arrayA + _arrayB;\n\n// _arrayC => [1,2,3,2,4]\n```\n\n**Example 2:**\n\n```sqf\n_arrayA = [1,2,3,2,4];\n_arrayB = [2,3];\n\n_arrayC = _arrayA - _arrayB;\n\n// _arrayC => [1,4]\n```"},{"heading":"String Operators","level":4,"markdown":"The scripting language offers one single string operator to concatenate strings. Both operands must be Strings.\nThe return value of a string operation is a String.\n\n**Binary string operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| + | Concatenation | `myString1 + myString2` |\n\n- `+` adds the second operand on the end of the first operand\n\n**Example:**\n\n```sqf\n_stringA = \"Hello \";\n_stringB = \"World!\";\n\n_stringC = _stringA + _stringB;\n\n// _stringC => \"Hello World!\"\n```"},{"heading":"Config Operators","level":4,"markdown":"**Config operators**\n\n| Operator | Name | Example |\n|---|---|---|\n| += | Append Array | `myProperty[] += {element_1, element_2};` |\n\n**Example:**\n\n```cpp\nclass B_Soldier_F;\nclass MySoldier: B_Soldier_F\n{\n    RespawnItems[] += {\"FirstAidKit\"}; //Adds additional FAK\n};\n\n/*\n    Original RespawnItems[] array in config viewer:\n    RespawnItems[] = {\"FirstAidKit\"};\n\n    Modified RespawnItems[] array in config viewer:\n    RespawnItems[] = {\"FirstAidKit\",\"FirstAidKit\"};\n*/\n```"},{"heading":"Order of Precedence","level":3,"markdown":"Order of operations, also called operator precedence, is a set of rules specifying which procedures should be performed first in a mathematical expression."},{"heading":"Precedence Overview","level":4,"markdown":"> [!NOTE]\n> - Highest precedence means highest priority\n> - Associativity is (then) done from left to right, for example `3 + 5 + 8` will be processed as `((3 + 5) + 8)`\n\n| Precedence | Type of Operator | Examples |\n|---|---|---|\n| 11 | Nular operators (commands with no arguments):<br>- `commandName` | - Variables<br>- Values<br>- `()` / `[]` / `{}` / `\"\"` / `''` -enclosed expressions |\n| 10 | Unary operators (commands with 1 argument):<br>- `commandName` a | - +a<br>- -a<br>- +array<br>- ! b<br>- not b |\n| 9 | Hash-select operator | - array # index |\n| 8 | Power operator | - a ^ b |\n| 7 | - Number multiplication, division and remainder<br>- atan2<br>- Config access (/) | - a * b<br>- a / b<br>- a % b<br>- a mod b<br>- a / b<br>- a atan2 b |\n| 6 | - Number addition and subtraction<br>- Array addition and subtraction<br>- String addition<br>- min and max commands | - a + b<br>- a - b<br>- arr1 + arr2<br>- arr1 - arr2<br>- str1 + str2<br>- a min b<br>- a max b |\n| 5 | - else (if-then-else structure) | N/A |\n| 4 | Binary operators (commands with 2 arguments):<br>- a `commandName` b | - setDir<br>- switch colon (:) |\n| 3 | - == and != comparisons<br>- Number comparisons<br>- Config access (>>) | - a == b<br>- a != b<br>- a > b<br>- a < b<br>- a >= b<br>- a <= b<br>- config >> name |\n| 2 | Logical and operator | - &&<br>- and |\n| 1 | Logical or operator | - \\|\\|<br>- or |"},{"heading":"Examples","level":4,"markdown":"| Input | Process | Comment |\n|---|---|---|\n| `1 + 2 * 3` | `1 + (2 * 3)` | result equals 7, and not 9 (see also [PEMDAS](https://en.wikipedia.org/wiki/Order_of_operations)) |\n| `sleep 10 + random 20` | `(sleep 10) + random 20` | `sleep 10` will return Nothing, then `+ random 20` will be calculated but not used.<br>`sleep (10 + random 20)` should be used instead |"},{"heading":"See Also","level":3,"markdown":"- SQF Syntax\n- SQS Syntax"}],"categories":["Syntax"],"groups":["Scripting"]}