{"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**\nAn operand is any value given to an operator.\n\n**Expression**\nAn expression is basically any code that returns a value. Read expression for more information.\n\n**Unary Operator**\nAn unary operator is an operator that requires only one operand.\n\nUnary operation:\n`*operator* expression`\n\n**Binary Operator**\nA binary operator is an operator that requires two operands.\n\nBinary operation:\n`expression *operator* expression`\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 identifier = expression\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|  |  |  |  |\n|---|---|---|---|\n| + Unary arithmetic operators (in order of precedence) | Operator | Name | Example |\n| - | Negation | ```sqf<br>-a<br>``` |  |\n| + | Duplication | ```sqf<br>+a<br>``` |  |\n| ( | Bracket | ```sqf<br>(expression)<br>``` |  |\n\n|  |  |  |  |\n|---|---|---|---|\n| + Binary arithmetic operators | Operator | Name | Example |\n| + | Addition | ```sqf<br>a + b<br>``` |  |\n| - | Subtraction | ```sqf<br>a - b<br>``` |  |\n| * | Multiplication | ```sqf<br>a * b<br>``` |  |\n| / | Division | ```sqf<br>a / b<br>``` |  |\n| % | Modulo | ```sqf<br>a % b<br>``` |  |\n| mod | Modulo | ```sqf<br>a mod b<br>``` |  |\n| ^ | Raise to the power of | ```sqf<br>a ^ b<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Unary logical operators | Operator | Name | Example |\n| ! | Not | ```sqf<br>!a<br>``` |  |\n| not | Not | ```sqf<br>not a<br>``` |  |\n\nThe `Not`-operator always returns the inverse Boolean value. If a Boolean `a` is `true`, `!a` returns `false` and vice versa.\n\n|  |  |  |  |\n|---|---|---|---|\n| + Binary logical operators | Operator | Name | Example |\n| && | And | ```sqf<br>a && b<br>``` |  |\n| and | And | ```sqf<br>a and b<br>``` |  |\n| \\|\\| | Or | ```sqf<br>a \\|\\| b<br>``` |  |\n| or | Or | ```sqf<br>a or b<br>``` |  |\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|  |  |  |\n|---|---|---|\n| + Combined logical operators | Name | Combination |\n| Xor | ```sqf<br>((a \\|\\| b) && !(a && b))<br>``` |  |\n| Nor | ```sqf<br>!(a \\|\\| b)<br>``` |  |\n| Nand | ```sqf<br>!(a && b)<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Comparison operators | Operator | Name | Example |\n| == | Equal | ```sqf<br>a == b<br>``` |  |\n| != | Not equal | ```sqf<br>a != b<br>``` |  |\n| < | Less than | ```sqf<br>a < b<br>``` |  |\n| > | Greater than | ```sqf<br>a > b<br>``` |  |\n| <= | Less or equal | ```sqf<br>a <= b<br>``` |  |\n| >= | Greater or equal | ```sqf<br>a >= b<br>``` |  |\n\n> [!NOTE]\n>\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|  |  |  |  |\n|---|---|---|---|\n| + Unary array operators | Operator | Name | Example |\n| + | Copy | ```sqf<br>+myArray<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Binary array operators | Operator | Name | Example |\n| + | Concatenation | ```sqf<br>myArray1 + myArray2<br>``` |  |\n| - | Removal | ```sqf<br>myArray1 - myArray2<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Binary string operators | Operator | Name | Example |\n| + | Concatenation | ```sqf<br>myString1 + myString2<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Config operators | Operator | Name | Example |\n| += | Append Array | ```cpp<br>myProperty[] += {element_1, element_2};<br>``` |  |\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\nSee Order of Precedence.\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**\nAn operand is any value given to an operator.\n\n**Expression**\nAn expression is basically any code that returns a value. Read expression for more information.\n\n**Unary Operator**\nAn unary operator is an operator that requires only one operand.\n\nUnary operation:\n`*operator* expression`\n\n**Binary Operator**\nA binary operator is an operator that requires two operands.\n\nBinary operation:\n`expression *operator* expression`"},{"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 identifier = expression\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|  |  |  |  |\n|---|---|---|---|\n| + Unary arithmetic operators (in order of precedence) | Operator | Name | Example |\n| - | Negation | ```sqf<br>-a<br>``` |  |\n| + | Duplication | ```sqf<br>+a<br>``` |  |\n| ( | Bracket | ```sqf<br>(expression)<br>``` |  |\n\n|  |  |  |  |\n|---|---|---|---|\n| + Binary arithmetic operators | Operator | Name | Example |\n| + | Addition | ```sqf<br>a + b<br>``` |  |\n| - | Subtraction | ```sqf<br>a - b<br>``` |  |\n| * | Multiplication | ```sqf<br>a * b<br>``` |  |\n| / | Division | ```sqf<br>a / b<br>``` |  |\n| % | Modulo | ```sqf<br>a % b<br>``` |  |\n| mod | Modulo | ```sqf<br>a mod b<br>``` |  |\n| ^ | Raise to the power of | ```sqf<br>a ^ b<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Unary logical operators | Operator | Name | Example |\n| ! | Not | ```sqf<br>!a<br>``` |  |\n| not | Not | ```sqf<br>not a<br>``` |  |\n\nThe `Not`-operator always returns the inverse Boolean value. If a Boolean `a` is `true`, `!a` returns `false` and vice versa.\n\n|  |  |  |  |\n|---|---|---|---|\n| + Binary logical operators | Operator | Name | Example |\n| && | And | ```sqf<br>a && b<br>``` |  |\n| and | And | ```sqf<br>a and b<br>``` |  |\n| \\|\\| | Or | ```sqf<br>a \\|\\| b<br>``` |  |\n| or | Or | ```sqf<br>a or b<br>``` |  |\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|  |  |  |\n|---|---|---|\n| + Combined logical operators | Name | Combination |\n| Xor | ```sqf<br>((a \\|\\| b) && !(a && b))<br>``` |  |\n| Nor | ```sqf<br>!(a \\|\\| b)<br>``` |  |\n| Nand | ```sqf<br>!(a && b)<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Comparison operators | Operator | Name | Example |\n| == | Equal | ```sqf<br>a == b<br>``` |  |\n| != | Not equal | ```sqf<br>a != b<br>``` |  |\n| < | Less than | ```sqf<br>a < b<br>``` |  |\n| > | Greater than | ```sqf<br>a > b<br>``` |  |\n| <= | Less or equal | ```sqf<br>a <= b<br>``` |  |\n| >= | Greater or equal | ```sqf<br>a >= b<br>``` |  |\n\n> [!NOTE]\n>"},{"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|  |  |  |  |\n|---|---|---|---|\n| + Unary array operators | Operator | Name | Example |\n| + | Copy | ```sqf<br>+myArray<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Binary array operators | Operator | Name | Example |\n| + | Concatenation | ```sqf<br>myArray1 + myArray2<br>``` |  |\n| - | Removal | ```sqf<br>myArray1 - myArray2<br>``` |  |\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|  |  |  |  |\n|---|---|---|---|\n| + Binary string operators | Operator | Name | Example |\n| + | Concatenation | ```sqf<br>myString1 + myString2<br>``` |  |\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":"|  |  |  |  |\n|---|---|---|---|\n| + Config operators | Operator | Name | Example |\n| += | Append Array | ```cpp<br>myProperty[] += {element_1, element_2};<br>``` |  |\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":"See Order of Precedence."},{"heading":"See Also","level":3,"markdown":"- SQF Syntax\n- SQS Syntax"}],"categories":["Syntax"],"groups":["Scripting"]}