Operators: Difference between revisions
(→Rules of Precedence: is now transclude from Order of Precedence) |
(Replaced operators table class with wikitable, fixed some of the huge spacings between content) |
||
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. | ||
Line 29: | Line 25: | ||
: Binary operation: | : Binary operation: | ||
: <code>[[expression]] ''operator'' [[expression]]</code> | : <code>[[expression]] ''operator'' [[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]] = [[expression]] | ||
'''Example 1:''' | '''Example 1:''' | ||
Line 47: | Line 37: | ||
You might think that this operator compares <tt>a</tt> and <tt>b</tt>, but that is not the case. <tt>=</tt> simply sets the left value to be the right one. There don't exist any other assignment operators like <tt>+=</tt>, <tt>-=</tt> etc., that can be found in other programming languages. | You might think that this operator compares <tt>a</tt> and <tt>b</tt>, but that is not the case. <tt>=</tt> simply sets the left value to be the right one. There don't exist any other assignment operators like <tt>+=</tt>, <tt>-=</tt> etc., that can be found in other programming languages. | ||
'''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. | ||
All operands of arithmetic operations must be [[Number|Numbers]]. Arithmetic operations always return a [[Number]]. | All operands of arithmetic operations must be [[Number|Numbers]]. Arithmetic operations always return a [[Number]]. | ||
{| class=" | {| class="wikitable" | ||
|+ Unary arithmetic operators (in order of precedence) | |+ Unary arithmetic operators (in order of precedence) | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
Line 69: | Line 56: | ||
|} | |} | ||
{| class=" | {| class="wikitable" | ||
|+ Binary arithmetic operators | |+ Binary arithmetic operators | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
Line 87: | Line 74: | ||
| ^ || Raise to the power of || <tt>a ^ b</tt> | | ^ || Raise to the power of || <tt>a ^ b</tt> | ||
|} | |} | ||
'''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]]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]]. | ||
{| class=" | {| class="wikitable" | ||
|+ Unary logical operators | |+ Unary logical operators | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
Line 111: | Line 92: | ||
The <tt>Not</tt>-operator always returns the inverse [[Boolean]] value. If a [[Boolean]] <tt>a</tt> is <tt>true</tt>, <tt>!a</tt> returns <tt>false</tt> and vice versa. | The <tt>Not</tt>-operator always returns the inverse [[Boolean]] value. If a [[Boolean]] <tt>a</tt> is <tt>true</tt>, <tt>!a</tt> returns <tt>false</tt> and vice versa. | ||
{| class=" | {| class="wikitable" | ||
|+ Binary logical operators | |+ Binary logical operators | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
Line 130: | Line 111: | ||
There is no Xor, Nor and Nand operator. Those can be simulated using the basic operators though: | There is no Xor, Nor and Nand operator. Those can be simulated using the basic operators though: | ||
{| class=" | {| class="wikitable" | ||
|+ Combined logical operators | |+ Combined logical operators | ||
! Name !! Combination | ! Name !! Combination | ||
Line 146: | Line 127: | ||
=== 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> | ||
Comparisons always return a [[Boolean]]: <tt>true</tt> if the comparison matches, <tt>false</tt> if not. | Comparisons always return a [[Boolean]]: <tt>true</tt> if the comparison matches, <tt>false</tt> if not. | ||
{| class=" | {| class="wikitable" | ||
|+ Comparison operators | |+ Comparison operators | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
Line 185: | Line 164: | ||
=== 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]]. | ||
{| class=" | {| class="wikitable" | ||
|+ Unary array operators | |+ Unary array operators | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
Line 200: | Line 177: | ||
'''Example 1:''' | '''Example 1:''' | ||
_arrayA = [1,2]; | _arrayA = [1,2]; | ||
_arrayB = _arrayA; | _arrayB = _arrayA; | ||
Line 209: | Line 185: | ||
'''Example 2:''' | '''Example 2:''' | ||
_arrayA = [1,2]; | _arrayA = [1,2]; | ||
_arrayB = +_arrayA; | _arrayB = +_arrayA; | ||
Line 217: | Line 192: | ||
_arrayB => [1,2] | _arrayB => [1,2] | ||
{| class=" | {| class="wikitable" | ||
|+ Binary array operators | |+ Binary array operators | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
Line 230: | Line 205: | ||
'''Example 1:''' | '''Example 1:''' | ||
_arrayA = [1,2]; | _arrayA = [1,2]; | ||
_arrayB = [3,2,4]; | _arrayB = [3,2,4]; | ||
Line 239: | Line 213: | ||
'''Example 2:''' | '''Example 2:''' | ||
_arrayA = [1,2,3,2,4]; | _arrayA = [1,2,3,2,4]; | ||
_arrayB = [2,3]; | _arrayB = [2,3]; | ||
Line 248: | Line 221: | ||
=== 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]]. | ||
The return value of a string operation is a [[String]]. | The return value of a string operation is a [[String]]. | ||
{| class=" | {| class="wikitable" | ||
|+ Binary string operators | |+ Binary string operators | ||
! Operator !! Name !! Example | ! Operator !! Name !! Example | ||
Line 263: | Line 234: | ||
'''Example:''' | '''Example:''' | ||
_stringA = "Hello "; | _stringA = "Hello "; | ||
_stringB = "World!"; | _stringB = "World!"; | ||
Line 276: | Line 246: | ||
== See Also == | == See Also == | ||
* [[SQF syntax]] | * [[SQF syntax]] | ||
[[Category: Syntax]] | [[Category: Syntax]] |
Revision as of 09:41, 11 February 2021
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.
Operator | Name | Example |
---|---|---|
- | Negation | -a, +-a, -+a |
+ | Posation | +a |
( | Bracket | (expression) |
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.
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 | 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:
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.
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 |
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:
_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]
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.
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!"
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. |