Operators: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(Added section for config operators | Added += config operator | Added examples for += config operator)
 
(62 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Stub}}
{{TOC|side}}
'''Operators''' are the base commands each programming language is built on. They provide ability to perform basic mathematical and logical operations.


Now you meet operators and expressions - foundation-stones of every scripting language.


==Expressions==
== Requirements ==


Every expression is created from operands and operators. Operators call interpreter what it should do with operands. For example: AKMagazines * AmmoPerMagazine says: "multiple value in variable AKMagazines with value in variable AmmoPerMagazine". We evaluate expressions for obtain outcomes. If you want to store it, you must evaluate statement. For example: AllAKAmmo = AKMagazines * AmmoPerMagazine. It's order to interpreter to multiple value in variable AKMagazines with value in variable AmmoPerMagazine and prouuct store in variable AllAKAmmo (if there is any value, it will be rewritten). You need to know that expressions are evaluated from left side to right side, in statements is evaluated right expression first and outcome is stored in variable in left side. Another typical statement is VictorSide = [[side]] Victor or CarDamage = [[damage]] car.
To understand this article, you should read the following articles:


===Evaluating for outcome or side effects===
* [[Variables]]


When interpreter evaluates expression, gives you its outcome. Im most of cases it gives to you nothing, but it change something in game. (
PrivateHonka [[setDamage]] 1
increase global IQ of all army solders (kill him) ''';-)''' ).


===Operators===
== Terms ==


Operators in <abbr title="Armed Assault Scripting Language; it isn't offical">ArmA SL</abbr> are implemented as commands (as everything). More important that previous information is that outcome of expression with arithmetic operators is number, when logical operators are used, its logical value (true or false (it's also commands which returns logical values. In other scripting languages (Python, etc.) is this value part of interpreter. (it's only small different, if you learn ArmA SL principles, you will be able to learn any other language (Python) easily). There you can see Arma SL has commands for everything.), it's data type is bool).
; Operand
: An [[Operand|operand]] is any value given to an operator.


*Arithmetical operators
; Expression
** [[a_plus_b|+]] // This operator can contact two strings too.
: An expression is basically any code that returns a value. Read [[Expression|expression]] for more information.
** [[a_-_b|-]]
** [[a_/_b|/]]
** [[a * b|*]]
** [[a % b|%]]
*Logical operators
** [[a_==_b|==]] // if operands on left and right side are the same, outcome is true
** [[a_!=_b|!=]] // it they are not the same, true
** [[! a|!]] //  it have one operand on left side. Outcome is operands negated value
** [[a less b|<]]
** [[a greater b|>]]
** [[a greater= b|>=]]
** [[a less= b|=<]]


; Unary Operator
: An unary operator is an operator that requires only one operand.
:
: Unary operation:
: <code style="display: block">''operator'' [[Expression|expression]]</code>


==Changing variable type==
; Binary Operator
: A binary operator is an operator that requires two operands.
:
: Binary operation:
: <code style="display: block">[[Expression|expression]] ''operator'' [[Expression|expression]]</code>


This operation is there limited, you can only retype to string with help of [[format]] command. Its primary function is similar to printf command in C - it replace %1, %2, %3... if occur in string given as 0. element of given array with outcome of expressions in first (%1), second(%2)... array elements - there is one different: it returns created array, not print it.


text = [[format]] ["Player side: %1 - Human players on that side: %2", [[side]] [[player]], [[playersNumber]] [[side]] [[player]]]
== Operators ==


Now we have string about situation in game. If you are confused you don't see expression in our example, know that 'value of expression where is only variable or number without operators is the same as variable or number value, howgh'.
=== Assignment Operators ===


== What's next? ==
Assignment operators are used to assign values to a [[Variables|variable]]. OFP's scripting language provides only one assignment operator.


control structures
'''Assignment:'''
[[Identifier|identifier]] = [[Expression|expression]]


[[ArmA: Variables|<< Variables]] | [[ArmA: Control Structures|Control Structures >>]]
'''Example 1:'''
<sqf>a = b</sqf>


[[Category:ArmA: Scripting|Operators]]
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:'''
<sqf>a = b * c</sqf>
 
=== Arithmetic Operators ===
 
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]].
 
{| class="wikitable"
|+ Unary arithmetic operators (in order of precedence)
! Operator !! Name !! Example
|-
| - || Negation || <sqf>-a</sqf>
|-
| + || Duplication || <sqf>+a</sqf>
|-
| ( || Bracket  || <sqf>(expression)</sqf>
|}
 
{| class="wikitable"
|+ Binary arithmetic operators
! Operator !! Name !! Example
|-
| + || Addition || <sqf>a + b</sqf>
|-
| - || Subtraction || <sqf>a - b</sqf>
|-
| * || Multiplication || <sqf>a * b</sqf>
|-
| / || Division || <sqf>a / b</sqf>
|-
| % || Modulo || <sqf>a % b</sqf>
|-
| mod || Modulo || <sqf>a mod b</sqf>
|-
| ^ || Raise to the power of || <sqf>a ^ b</sqf>
|}
 
{{Feature|informative|See also [[:Category:Command Group: Math|Math Commands]].}}
 
=== Logical Operators ===
 
Logical operators evaluate [[Boolean]] values. All operands of logical operations are [[Boolean]]s.
A logical operation always returns a [[Boolean]].
 
{| class="wikitable"
|+ Unary logical operators
! Operator !! Name !! Example
|-
| ! || Not || <sqf>!a</sqf>
|-
| not || Not || <sqf>not a</sqf>
|}
 
The {{hl|Not}}-operator always returns the inverse [[Boolean]] value. If a [[Boolean]] {{hl|a}} is {{hl|true}}, {{hl|!a}} returns {{hl|false}} and vice versa.
 
{| class="wikitable"
|+ Binary logical operators
! Operator !! Name !! Example
|-
| && || And || <sqf>a && b</sqf>
|-
| and || And || <sqf>a and b</sqf>
|-
| <nowiki>||</nowiki> || Or || <sqf>a || b</sqf>
|-
| or || Or || <sqf>a or b</sqf>
|}
 
* {{hl|And}} only returns {{hl|true}} if ''both'' operands are {{hl|true}}
* {{hl|Or}} returns {{hl|true}} if ''one or both'' operands are {{hl|true}}
 
 
There is no Xor, Nor and Nand operator. Those can be simulated using the basic operators though:
 
{| class="wikitable"
|+ Combined logical operators
! Name !! Combination
|-
| Xor || <sqf>((a || b) && !(a && b))</sqf>
|-
| Nor || <sqf>!(a || b)</sqf>
|-
| Nand || <sqf>!(a && b)</sqf>
|}
 
* {{hl|Xor}} returns {{hl|true}} if ''exactly one'' of both values is {{hl|true}}
* {{hl|Nor}} returns {{hl|true}} if ''none'' of both values is {{hl|true}}
* {{hl|Nand}} returns {{hl|true}} if ''not both'' values are {{hl|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 {{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.
 
{| class="wikitable"
|+ Comparison operators
! Operator !! Name !! Example
|-
| == || Equal || <sqf>a == b</sqf>
|-
| != || Not equal || <sqf>a != b</sqf>
|-
| < || Less than || <sqf>a < b</sqf>
|-
| > || Greater than || <sqf>a > b</sqf>
|-
| <= || Less or equal || <sqf>a <= b</sqf>
|-
| >= || Greater or equal || <sqf>a >= b</sqf>
|}
 
{{Feature|informative|2=
{{{!}} class="wikitable" style="float: right"
! invalid before {{arma3}} v2.00
! 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><!--
-->Comparing a [[Boolean]] value with {{hl|[[true]]}} is the same as the value itself.<br><!--
-->Comparing a [[Boolean]] value with {{hl|[[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]].
 
{| class="wikitable"
|+ Unary array operators
! Operator !! Name !! Example
|-
| + || Copy || <sqf>+myArray</sqf>
|}
 
Normally arrays are assigned ''by reference''. That means, if you assign array {{hl|a}} to array {{hl|b}} and change {{hl|a}} afterwards, also {{hl|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]
</sqf>
 
'''Example 2:'''
<sqf>
_arrayA = [1,2];
_arrayB = +_arrayA;
_arrayA set [0,5];
 
// _arrayA => [5,2]
// _arrayB => [1,2]
</sqf>
 
{| class="wikitable"
|+ Binary array operators
! Operator !! Name !! Example
|-
| + || Concatenation || <sqf>myArray1 + myArray2</sqf>
|-
| - || Removal || <sqf>myArray1 - myArray2</sqf>
|}
 
* {{hl|+}} adds the second operand on the end of the first operand
* {{hl|-}} 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]
</sqf>
 
'''Example 2:'''
<sqf>
_arrayA = [1,2,3,2,4];
_arrayB = [2,3];
 
_arrayC = _arrayA - _arrayB;
 
// _arrayC => [1,4]
</sqf>
 
=== String Operators ===
 
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]].
 
{| class="wikitable"
|+ Binary string operators
! Operator !! Name !! Example
|-
| + || Concatenation || <sqf>myString1 + myString2</sqf>
|}
 
* {{hl|+}} adds the second operand on the end of the first operand
 
'''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}}
 
 
== See Also ==
 
* [[SQF Syntax]]
* [[SQS 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:

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/SQS.

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
+ Duplication
+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
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
!a
You could not compare Boolean values with == before Arma 3 v2.00 (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!"

Config Operators

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

  • Highest precedence means highest priority
  • Associativity is (then) done from left to right, for example 3 + 5 + 8 will be processed as ((3 + 5) + 8)
Precedence Type of Operator Examples
11

Nular operators (commands with no arguments):

  • commandName
10

Unary operators (commands with 1 argument):

  • commandName a
9 Hash-select operator
8 Power operator
7
6
5 N/A
4

Binary operators (commands with 2 arguments):

  • a commandName b
3
2 Logical and operator
1 Logical or operator


Examples

Input Process Comment
1 + 2 * 3
1 + (2 * 3)
result equals 7, and not 9 (see also PEMDAS)
sleep 10 + random 20
(sleep 10) + random 20
sleep 10 will return Nothing, then + random 20 will be calculated but not used.

sleep (10 + random 20) should be used instead


See Also