Operators: Difference between revisions
Lou Montana (talk | contribs) |
Hypoxic125 (talk | contribs) (Added section for config operators | Added += config operator | Added examples for += config operator) |
||
(3 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
{{TOC|side}} | {{TOC|side}} | ||
'''Operators''' are the base commands each programming language is built on. They provide ability to perform basic mathematical and logical operations. | '''Operators''' are the base commands each programming language is built on. They provide ability to perform basic mathematical and logical operations. | ||
== Requirements == | == Requirements == | ||
To understand this article, you should read the following articles: | To understand this article, you should read the following articles: | ||
* [[Variables]] | * [[Variables]] | ||
== Terms == | == Terms == | ||
; Operand | ; Operand | ||
: An [[Operand|operand]] is any value given to an operator. | : An [[Operand|operand]] is any value given to an operator. | ||
; Expression | ; Expression | ||
: An expression is basically any code that returns a value. Read [[Expression]] for more information. | : An expression is basically any code that returns a value. Read [[Expression|expression]] for more information. | ||
; Unary Operator | ; Unary Operator | ||
Line 18: | Line 22: | ||
: | : | ||
: Unary operation: | : Unary operation: | ||
: <code>''operator'' [[expression]]</code> | : <code style="display: block">''operator'' [[Expression|expression]]</code> | ||
; Binary Operator | ; Binary Operator | ||
Line 24: | Line 28: | ||
: | : | ||
: Binary operation: | : Binary operation: | ||
: <code>[[expression]] ''operator'' [[expression]]</code> | : <code style="display: block">[[Expression|expression]] ''operator'' [[Expression|expression]]</code> | ||
== Operators == | == Operators == | ||
=== Assignment Operators === | === Assignment Operators === | ||
Assignment operators are used to assign values to a [[Variables|variable]]. OFP's scripting language provides only one assignment operator. | Assignment operators are used to assign values to a [[Variables|variable]]. OFP's scripting language provides only one assignment operator. | ||
'''Assignment:''' | '''Assignment:''' | ||
[[identifier]] = [[expression]] | [[Identifier|identifier]] = [[Expression|expression]] | ||
'''Example 1:''' | '''Example 1:''' | ||
<sqf>a = b</sqf> | |||
You might think that this operator compares {{hl|a}} and {{hl|b}}, but that is not the case. {{hl|{{=}}}} simply sets the left value to be the right one. | You might think that this operator compares {{hl|a}} and {{hl|b}}, but that is not the case. {{hl|{{=}}}} simply sets the left value to be the right one. Other assignment operators like {{hl|c= +=}}, {{hl|c= -=}} that can be found in other programming languages do not exist in [[SQF Syntax|SQF]]/[[SQS Syntax|SQS]]. | ||
'''Example 2:''' | '''Example 2:''' | ||
<sqf>a = b * c</sqf> | |||
=== Arithmetic Operators === | === Arithmetic Operators === | ||
Remember arithmetic operations from school? These work just the same way. | Remember arithmetic operations from school? These work just the same way. | ||
All operands of arithmetic operations must be [[Number]]s. Arithmetic operations always return a [[Number]]. | All operands of arithmetic operations must be [[Number]]s. Arithmetic operations always return a [[Number]]. | ||
Line 49: | Line 57: | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
|- | |- | ||
| - || Negation || | | - || Negation || <sqf>-a</sqf> | ||
|- | |- | ||
| + || | | + || Duplication || <sqf>+a</sqf> | ||
|- | |- | ||
| ( || Bracket || | | ( || Bracket || <sqf>(expression)</sqf> | ||
|} | |} | ||
Line 60: | Line 68: | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
|- | |- | ||
| + || Addition || | | + || Addition || <sqf>a + b</sqf> | ||
|- | |- | ||
| - || Subtraction || | | - || Subtraction || <sqf>a - b</sqf> | ||
|- | |- | ||
| * || Multiplication || | | * || Multiplication || <sqf>a * b</sqf> | ||
|- | |- | ||
| / || Division || | | / || Division || <sqf>a / b</sqf> | ||
|- | |- | ||
| % || Modulo || | | % || Modulo || <sqf>a % b</sqf> | ||
|- | |- | ||
| mod || Modulo || | | mod || Modulo || <sqf>a mod b</sqf> | ||
|- | |- | ||
| ^ || Raise to the power of || | | ^ || Raise to the power of || <sqf>a ^ b</sqf> | ||
|} | |} | ||
{{Feature|informative|See also [[:Category:Command Group: Math|Math Commands]].}} | |||
=== Logical Operators === | === Logical Operators === | ||
Logical operators evaluate [[Boolean]] values. All operands of logical operations are [[Boolean]]s. | Logical operators evaluate [[Boolean]] values. All operands of logical operations are [[Boolean]]s. | ||
A logical operation always returns a [[Boolean]]. | A logical operation always returns a [[Boolean]]. | ||
Line 85: | Line 94: | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
|- | |- | ||
| ! || Not || | | ! || Not || <sqf>!a</sqf> | ||
|- | |- | ||
| not || Not || | | not || Not || <sqf>not a</sqf> | ||
|} | |} | ||
Line 96: | Line 105: | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
|- | |- | ||
| && || And || | | && || And || <sqf>a && b</sqf> | ||
|- | |- | ||
| and || And || | | and || And || <sqf>a and b</sqf> | ||
|- | |- | ||
| <nowiki>||</nowiki> || Or || | | <nowiki>||</nowiki> || Or || <sqf>a || b</sqf> | ||
|- | |- | ||
| or || Or || | | or || Or || <sqf>a or b</sqf> | ||
|} | |} | ||
Line 115: | Line 124: | ||
! Name !! Combination | ! Name !! Combination | ||
|- | |- | ||
| Xor || | | Xor || <sqf>((a || b) && !(a && b))</sqf> | ||
|- | |- | ||
| Nor || | | Nor || <sqf>!(a || b)</sqf> | ||
|- | |- | ||
| Nand || | | Nand || <sqf>!(a && b)</sqf> | ||
|} | |} | ||
Line 127: | Line 136: | ||
=== Comparison Operators === | === Comparison Operators === | ||
Comparison operators compare two values. Operands of comparisons may be of type [[Number]], [[Side]], [[String]], [[Object]], [[Group]], [[Structured Text]], [[Config]], [[Display]] or [[Control]] for {{hl|c= ==}} and {{hl|! | |||
Comparison operators compare two values. Operands of comparisons may be of type [[Number]], [[Side]], [[String]], [[Object]], [[Group]], [[Structured Text]], [[Config]], [[Display]] or [[Control]] for {{hl|c= ==}} and {{hl|c= !=}}, and [[Number]] for {{hl|c= < > >= <=}}. | |||
Comparisons always return a [[Boolean]]: {{hl|true}} if the comparison matches, {{hl|false}} if not. | Comparisons always return a [[Boolean]]: {{hl|true}} if the comparison matches, {{hl|false}} if not. | ||
Line 134: | Line 144: | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
|- | |- | ||
| == || Equal || | | == || Equal || <sqf>a == b</sqf> | ||
|- | |- | ||
| != || Not equal || | | != || Not equal || <sqf>a != b</sqf> | ||
|- | |- | ||
| < || Less than || | | < || Less than || <sqf>a < b</sqf> | ||
|- | |- | ||
| > || Greater than || | | > || Greater than || <sqf>a > b</sqf> | ||
|- | |- | ||
| <= || Less or equal || | | <= || Less or equal || <sqf>a <= b</sqf> | ||
|- | |- | ||
| >= || Greater or equal || | | >= || Greater or equal || <sqf>a >= b</sqf> | ||
|} | |} | ||
{{Feature | | {{Feature|informative|2= | ||
{{{!}} class | {{{!}} class="wikitable" style="float: right" | ||
! invalid before {{arma3}} v2.00 | ! invalid before {{arma3}} v2.00 | ||
! valid in all versions | ! valid in all versions | ||
{{!}}- | {{!}}- | ||
{{!}} < | {{!}} <sqf>a == true</sqf> | ||
{{!}} < | {{!}} <sqf>a</sqf> | ||
{{!}}- | {{!}}- | ||
{{!}} < | {{!}} <sqf>a == false</sqf> | ||
{{!}} < | {{!}} <sqf>!a</sqf> | ||
{{!}}} | {{!}}} | ||
You could not compare [[Boolean]] values with [[a {{=}}{{=}} b|{{=}}{{=}}]] before '''{{arma3}} v2.00''' (in {{arma3}} before that, use [[isEqualTo]]).<br><!-- | You could not compare [[Boolean]] values with [[a {{=}}{{=}} b|{{=}}{{=}}]] before '''{{arma3}} v2.00''' (in {{arma3}} before that, use [[isEqualTo]]).<br><!-- | ||
Line 164: | Line 174: | ||
=== Array Operators === | === Array Operators === | ||
The scripting language offers own operators to deal with arrays. All operands, of course, have to be of type [[Array]]. | The scripting language offers own operators to deal with arrays. All operands, of course, have to be of type [[Array]]. | ||
The return value of an array operation is an [[Array]]. | The return value of an array operation is an [[Array]]. | ||
Line 171: | Line 182: | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
|- | |- | ||
| + || Copy || | | + || Copy || <sqf>+myArray</sqf> | ||
|} | |} | ||
Line 177: | Line 188: | ||
'''Example 1:''' | '''Example 1:''' | ||
<sqf> | |||
_arrayA = [1,2]; | |||
_arrayB = _arrayA; | |||
_arrayA set [0,5]; | |||
// _arrayA => [5,2] | |||
// _arrayB => [5,2] | |||
</sqf> | |||
'''Example 2:''' | '''Example 2:''' | ||
<sqf> | |||
_arrayA = [1,2]; | |||
_arrayB = +_arrayA; | |||
_arrayA set [0,5]; | |||
// _arrayA => [5,2] | |||
// _arrayB => [1,2] | |||
</sqf> | |||
{| class="wikitable" | {| class="wikitable" | ||
Line 196: | Line 211: | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
|- | |- | ||
| + || Concatenation || | | + || Concatenation || <sqf>myArray1 + myArray2</sqf> | ||
|- | |- | ||
| - || Removal || | | - || Removal || <sqf>myArray1 - myArray2</sqf> | ||
|} | |} | ||
Line 205: | Line 220: | ||
'''Example 1:''' | '''Example 1:''' | ||
<sqf> | |||
_arrayA = [1,2]; | |||
_arrayB = [3,2,4]; | |||
_arrayC = _arrayA + _arrayB; | |||
// _arrayC => [1,2,3,2,4] | |||
</sqf> | |||
'''Example 2:''' | '''Example 2:''' | ||
<sqf> | |||
_arrayA = [1,2,3,2,4]; | |||
_arrayB = [2,3]; | |||
_arrayC = _arrayA - _arrayB; | |||
// _arrayC => [1,4] | |||
</sqf> | |||
=== String Operators === | === String Operators === | ||
The scripting language offers one single string operator to concatenate strings. Both operands must be [[String]]s. | The scripting language offers one single string operator to concatenate strings. Both operands must be [[String]]s. | ||
The return value of a string operation is a [[String]]. | The return value of a string operation is a [[String]]. | ||
Line 228: | Line 248: | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
|- | |- | ||
| + || Concatenation || | | + || Concatenation || <sqf>myString1 + myString2</sqf> | ||
|} | |} | ||
Line 234: | Line 254: | ||
'''Example:''' | '''Example:''' | ||
<sqf> | |||
_stringA = "Hello "; | |||
_stringB = "World!"; | |||
_stringC = _stringA + _stringB; | |||
// _stringC => "Hello World!" | |||
</sqf> | |||
=== Config Operators === | |||
{| class="wikitable" | |||
|+ Config operators | |||
! Operator !! Name !! Example | |||
|- | |||
| += || Append Array || <syntaxhighlight lang="cpp">myProperty[] += {element_1, element_2};</syntaxhighlight> | |||
|} | |||
'''Example:''' | |||
<syntaxhighlight lang="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"}; | |||
*/ | |||
</syntaxhighlight> | |||
== Order of Precedence == | == Order of Precedence == | ||
{{:Order of Precedence}} | {{:Order of Precedence}} | ||
== See Also == | == See Also == | ||
* [[SQF Syntax]] | * [[SQF Syntax]] | ||
* [[SQS Syntax]] | |||
[[Category: Syntax]] | [[Category: Syntax]] |
Latest revision as of 04:49, 9 April 2024
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:
Terms
- Operand
- An operand is any value given to an operator.
- Expression
- An expression is basically any code that returns a value. Read expression 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. OFP's scripting language provides only one assignment operator.
Assignment:
identifier = expression
Example 1:
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/SQS.
Example 2:
Arithmetic Operators
Remember arithmetic operations from school? These work just the same way. All operands of arithmetic operations must be Numbers. Arithmetic operations always return a Number.
Operator | Name | Example |
---|---|---|
- | Negation | -a |
+ | Duplication | +a |
( | Bracket | (expression) |
Operator | Name | Example |
---|---|---|
+ | Addition | |
- | Subtraction | |
* | Multiplication | |
/ | Division | |
% | Modulo | |
mod | Modulo | |
^ | Raise to the power of |
Logical Operators
Logical operators evaluate Boolean values. All operands of logical operations are Booleans. A logical operation always returns a Boolean.
Operator | Name | Example |
---|---|---|
! | Not | !a |
not | Not | not a |
The Not-operator always returns the inverse Boolean value. If a Boolean a is true, !a returns false and vice versa.
Operator | Name | Example |
---|---|---|
&& | And | |
and | And | |
|| | Or | |
or | Or |
- 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:
Name | Combination |
---|---|
Xor | |
Nor | |
Nand |
- 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, Side, String, Object, Group, Structured Text, Config, Display or Control for == and !=, and Number for < > >= <=. Comparisons always return a Boolean: true if the comparison matches, false if not.
Operator | Name | Example |
---|---|---|
== | Equal | |
!= | Not equal | |
< | Less than | |
> | Greater than | |
<= | Less or equal | |
>= | Greater or equal |
Array Operators
The scripting language offers own operators to deal with arrays. All operands, of course, have to be of type Array. The return value of an array operation is an Array.
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:
Example 2:
Operator | Name | Example |
---|---|---|
+ | Concatenation | |
- | Removal |
- + adds the second operand on the end of the first operand
- - removes all elements of the second operand from the first operand
Example 1:
Example 2:
String Operators
The scripting language offers one single string operator to concatenate strings. Both operands must be Strings. The return value of a string operation is a String.
Operator | Name | Example |
---|---|---|
+ | Concatenation |
- + adds the second operand on the end of the first operand
Example:
Config Operators
Operator | Name | Example |
---|---|---|
+= | Append Array | myProperty[] += {element_1, element_2};
|
Example:
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
Precedence | Type of Operator | Examples |
---|---|---|
11 |
Nular operators (commands with no arguments):
|
|
10 |
Unary operators (commands with 1 argument):
|
|
9 | Hash-select operator | |
8 | Power operator | |
7 | ||
6 | ||
5 | N/A | |
4 |
Binary operators (commands with 2 arguments):
|
|
3 | ||
2 | Logical and operator | |
1 | Logical or operator |
Examples
Input | Process | Comment |
---|---|---|
result equals 7, and not 9 (see also PEMDAS) | ||
sleep 10 will return Nothing, then + random 20 will be calculated but not used. |