Operators: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(→‎Comparison Operators: clarify that you cannot use < > on anything but numbers.)
(Add Rules of Precedence from SQF syntax)
Line 1: Line 1:
{{SideTOC}}
'''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 ==
Line 6: Line 8:


* [[Variables]]
* [[Variables]]


== Terms ==
== Terms ==
Line 27: Line 30:
: <code>[[expression]] ''operator'' [[expression]]</code>
: <code>[[expression]] ''operator'' [[expression]]</code>


== Assignment Operators ==
 
== 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.
Line 38: Line 44:


'''Example 1:'''
'''Example 1:'''
  a = b
  a = b


Line 45: Line 50:


'''Example 2:'''
'''Example 2:'''
  a = b*c
  a = b*c


== 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.
Line 87: Line 91:




'''See also:''' [[:Category:Command_Group:_Math|Math Commands]]
'''See also:''' [[:Category:Command Group: Math|Math Commands]]
 


== Logical Operators ==
=== Logical Operators ===


Logical operators evaluate [[Boolean]] values. All operands of logical operations are [[Boolean|Booleans]].
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 140: Line 145:
* <tt>Nand</tt> returns <tt>true</tt> if ''not both'' values are <tt>true</tt> at the same time
* <tt>Nand</tt> returns <tt>true</tt> if ''not both'' values are <tt>true</tt> at the same time


== 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 <tt>==</tt> and <tt>!=</tt> , and [[Number]] for <tt>< > >= <= </tt>
Comparison operators compare two values. Operands of comparisons may be of type [[Number]], [[Side]], [[String]], [[Object]], [[Group]], [[Structured Text]], [[Config]], [[Display]] or [[Control]] for <tt>==</tt> and <tt>!=</tt> , and [[Number]] for <tt>< > >= <= </tt>
Line 163: Line 168:
|}
|}


'''Note:''' You can't compare [[Boolean]] values. Comparing a [[Boolean]] value with <tt>true</tt> is the same as the value itself. Comparing a [[Boolean]] value with <tt>false</tt> is the same as the ''inverse'' value.
{{Informative |
 
{{{!}} class{{=}}"wikitable" style{{=}}"float: right"
a == true
! invalid before {{arma3}} v2.00
// invalid, but equal to
! valid in all versions
a
{{!}}-
 
{{!}} <code>a {{=}}{{=}} [[true]]</code>
a == false
{{!}} <code>a</code>
// invalid, but equal to
{{!}}-
!a
{{!}} <code>a {{=}}{{=}} [[false]]</code>
{{!}} <code>!a</code>
{{!}}}
You could not compare [[Boolean]] values with [[a {{=}}{{=}} b|{{=}}{{=}}]] before '''{{arma3}} v1.99.146526''' (in {{arma3}} before that, use [[isEqualTo]]).<br><!--
-->Comparing a [[Boolean]] value with <tt>[[true]]</tt> is the same as the value itself.<br><!--
-->Comparing a [[Boolean]] value with <tt>[[false]]</tt> is the same as the ''inverse'' value.
}}


== 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]].
Line 236: Line 247:
  _arrayC => [1,4]
  _arrayC => [1,4]


== String Operators ==
=== String Operators ===


The scripting language offers one single string operator to concatenate strings. Both operands must be [[String|Strings]].
The scripting language offers one single string operator to concatenate strings. Both operands must be [[String|Strings]].
Line 259: Line 270:
   
   
  _stringC => "Hello World!"
  _stringC => "Hello World!"
== Rules of Precedence ==
{{Informative | Highest precedence {{=}} highest priority.}}
{| class="operators"
|-
! Prece-<br>dence
! Type of Operator
! Associativity
|-
| style="text-align: center" | 11
|
Nular operators (commands with no arguments):
* <tt>commandName</tt>
Other:
* Variables
* Values
* Braced expressions
| Left-to-right
|-
| style="text-align: center" | 10
|
Unary operators (commands with 1 argument):
* <tt>commandName</tt> a<br>
Unary plus and minus:
* [[+ |+a]]
* [[- | -a]]
[[Array]] copy operator:
* [[+ | +array]]
Logical <tt>NOT</tt> operator:
* [[!_a | !b]]
* [[not | not b]]
| Left-to-right
|-
| style="text-align: center" | 9
|
The new select operator:
* [[a_hash_b| array # index]]
| Left-to-right
|-
| style="text-align: center" | 8
|
Power operator:
* [[a ^ b]]
| Left-to-right
|-
| style="text-align: center" | 7
|
[[Number]] multiplication, division and remainder:
*[[a * b]]
* [[a / b]]
* [[a % b]]
* [[mod | a mod b]]
[[Config]] access:
* [[config / name]]
<tt>atan2</tt> command:
* [[atan2| a atan2 b]]
| Left-to-right
|-
| style="text-align: center" | 6
|
[[Number]] addition and subtraction:
* [[+ | a + b]]
* [[- | a - b]]
[[Array]] addition and subtraction:
* [[+ |  arr1 + arr2]]
* [[- | arr1 - arr2]]
[[String]] addition:
* [[+ | str1 + str2]]
<tt>min</tt> and <tt>max</tt> commands:
* [[min | a min b]]
* [[max | a max b]]
| Left-to-right
|-
| style="text-align: center" | 5
|
<tt>else</tt> operator:
* [[else]]
| Left-to-right
|-
| style="text-align: center" | 4
|
Binary operators (commands with 2 arguments):
* a <tt>commandName</tt> b
<tt>switch</tt> colon operator:
* [[a_:_b | : ]]
| Left-to-right
|-
| style="text-align: center" | 3
|
[[Number]] comparison:
* [[a == b]]
* [[a != b]]
* [[a_greater_b| a &gt; b]]
* [[a_less_b| a &lt; b]]
* [[a_less=_b| a &gt;= b]]
* [[a_less=_b| a &lt;= b]]
[[String]] comparison:
* [[a == b | str1 == str2]]
* [[a != b | str1 != str2]]
[[Group]], [[Side]], [[Object]], [[Structured Text]], [[Config]], [[Display]], [[Control]] or [[Location]] comparison:
* [[a == b]]
* [[a != b]]
[[Config]] access:
* [[config_greater_greater_name | config >> name]]
| Left-to-right
|-
| style="text-align: center" | 2
|
Logical <tt>AND</tt> operator:
* [[and| a && b]]
* [[and | a and b]]
| Left-to-right
|-
| style="text-align: center" | 1
|
Logical <tt>OR</tt> operator:
* [[a or b| a <nowiki>||</nowiki> b]]
* [[a or b| a or b]]
| Left-to-right
|}
== See Also ==
* [[SQF syntax]]


[[Category: Syntax]]
[[Category: Syntax]]
[[Category: Scripting_Topics]]
[[Category: Scripting Topics]]

Revision as of 13:04, 23 October 2020

Template:SideTOC 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:

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. There don't exist any other assignment operators like +=, -= etc., that can be found in other programming languages.


Example 2:

a = b*c

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.

Unary arithmetic operators (in order of precedence)
Operator Name Example
- Negation -a, +-a, -+a
+ Posation +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

Modulo returns the remainder of the division a / b.


See also: Math Commands


Logical Operators

Logical operators evaluate Boolean values. All operands of logical operations are Booleans.

A logical operation always returns a Boolean.

Unary logical operators
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.

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

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
invalid before Arma 3 v2.00 valid in all versions
a == true a
a == false !a
You could not compare Boolean values with == before Arma 3 v1.99.146526 (in Arma 3 before that, use isEqualTo).
Comparing a Boolean value with true is the same as the value itself.
Comparing a Boolean 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.

The return value of an array operation is an Array.

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:

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

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

Example 2:

_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:

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

_arrayC = _arrayA + _arrayB;

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

Example 2:

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

The return value of a string operation is a String.

Binary string operators
Operator Name Example
+ Concatenation myString1 + myString2
  • + adds the second operand on the end of the first operand

Example:

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

_stringC = _stringA + _stringB;

_stringC => "Hello World!"


Rules of Precedence

Highest precedence = highest priority.
Prece-
dence
Type of Operator Associativity
11

Nular operators (commands with no arguments):

  • commandName

Other:

  • Variables
  • Values
  • Braced expressions
Left-to-right
10

Unary operators (commands with 1 argument):

  • commandName a

Unary plus and minus:

Array copy operator:

Logical NOT operator:

Left-to-right
9

The new select operator:

Left-to-right
8

Power operator:

Left-to-right
7

Number multiplication, division and remainder:

Config access:

atan2 command:

Left-to-right
6

Number addition and subtraction:

Array addition and subtraction:

String addition:

min and max commands:

Left-to-right
5

else operator:

Left-to-right
4

Binary operators (commands with 2 arguments):

  • a commandName b

switch colon operator:

Left-to-right
3

Number comparison:

String comparison:

Group, Side, Object, Structured Text, Config, Display, Control or Location comparison:

Config access:

Left-to-right
2

Logical AND operator:

Left-to-right
1

Logical OR operator:

Left-to-right


See Also