Order of Precedence: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (added examples)
m (Fix Wikipedia link)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Introduction ==
<onlyinclude>
<onlyinclude>
Order of operations, also called operator precedence, is a set of rules specifying which procedures should be performed first in a mathematical expression.
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 Overview === <!--- Level 3 because it's transcluded into Operators page --->
 
{{Feature | Informative |
{{Feature | Informative |
* Highest precedence means highest priority
* Highest precedence means highest priority
* Associativity is (then) done from left to right, for example {{Inline code|3 + 5 + 8}} will be processed as {{Inline code|((3 + 5) + 8)}}
* Associativity is (then) done from left to right, for example <sqf inline>3 + 5 + 8</sqf> will be processed as <sqf inline>((3 + 5) + 8)</sqf>
}}
}}


Line 18: Line 18:
|
|
Nular operators (commands with no arguments):
Nular operators (commands with no arguments):
* <tt>commandName</tt>
* {{hl|commandName}}
|
|
* [[Variables]]
* [[Variables]]
Line 27: Line 27:
|
|
Unary operators (commands with 1 argument):
Unary operators (commands with 1 argument):
* <tt>commandName</tt> a
* {{hl|commandName}} a
|
| {{Columns|2|
<div style="columns: 2">
* [[+|+a]]
* [[+ |+a]]
* [[-|-a]]
* [[- | -a]]
* [[+|+array]]
* [[+ | +array]]
* [[! a|! b]]
* [[!_a | !b]]
* [[not|not b]]
* [[not | not b]]
}}
</div>
|-
|-
| style="text-align: center" | 9
| style="text-align: center" | 9
| Hash-select operator
| Hash-select operator
|
|
* [[a_hash_b| array # index]]
* [[a hash b|array # index]]
|-
|-
| style="text-align: center" | 8
| style="text-align: center" | 8
Line 51: Line 50:
* [[Number]] multiplication, division and remainder
* [[Number]] multiplication, division and remainder
* [[atan2]]
* [[atan2]]
* [[Config]] access ([[config / name|/]])
* [[Config]] access ([[a / b|/]])
|
| {{Columns|2|
<div style="columns: 2">
* [[a * b]]
* [[a * b]]
* [[a / b]]
* [[a / b]]
* [[a % b]]
* [[a % b]]
* [[mod | a mod b]]
* [[mod | a mod b]]
* [[config / name]]
* [[a / b]]
* [[atan2| a atan2 b]]
* [[atan2| a atan2 b]]
</div>
}}
|-
|-
| style="text-align: center" | 6
| style="text-align: center" | 6
Line 68: Line 66:
* [[String]] addition
* [[String]] addition
* [[min]] and [[max]] commands
* [[min]] and [[max]] commands
|
| {{Columns|2|
<div style="columns: 2">
* [[+|a + b]]
* [[+ | a + b]]
* [[-|a - b]]
* [[- | a - b]]
* [[+|arr1 + arr2]]
* [[+ | arr1 + arr2]]
* [[-|arr1 - arr2]]
* [[- | arr1 - arr2]]
* [[+|str1 + str2]]
* [[+ | str1 + str2]]
* [[min|a min b]]
* [[min | a min b]]
* [[max|a max b]]
* [[max | a max b]]
}}
</div>
|-
|-
| style="text-align: center" | 5
| style="text-align: center" | 5
Line 87: Line 84:
|
|
Binary operators (commands with 2 arguments):
Binary operators (commands with 2 arguments):
* a <tt>commandName</tt> b
* a {{hl|commandName}} b
|
|
* [[setDir]]
* [[setDir]]
* [[a_:_b |switch colon(:)]]
* [[a : b|switch colon (:)]]
|-
|-
| style="text-align: center" | 3
| style="text-align: center" | 3
Line 98: Line 95:
* [[Config]] access ([[config greater greater name|&gt;&gt;]])
* [[Config]] access ([[config greater greater name|&gt;&gt;]])
|
|
<div style="columns: 2">
{{Columns|2|
* [[a == b]]
* [[a == b]]
* [[a != b]]
* [[a != b]]
* [[a_greater_b| a &gt; b]]
* [[a greater b|a &gt; b]]
* [[a_less_b| a &lt; b]]
* [[a less b|a &lt; b]]
* [[a_less=_b| a &gt;= b]]
* [[a greater= b|a &gt;= b]]
* [[a_less=_b| a &lt;= b]]
* [[a less= b|a &lt;= b]]
</div>
}}
* [[config_greater_greater_name | config >> name]]
* [[config greater greater name|config >> name]]
|-
|-
| style="text-align: center" | 2
| style="text-align: center" | 2
Line 121: Line 118:
|}
|}


== Examples ==
 
=== Examples ===
 
{| class="wikitable"
{| class="wikitable"
! Input
! Input
Line 127: Line 126:
! Comment
! Comment
|-
|-
|
| <sqf>1 + 2 * 3</sqf>
1 + 2 * 3
| <sqf>1 + (2 * 3)</sqf>
|
| result equals 7, and not 9 (see also {{Link|https://en.wikipedia.org/wiki/Order_of_operations|PEMDAS}})
1 + (2 * 3)
| result equals 7, and not 9 (see also {{Wikipedia|Order of operations|PEMDAS}})
|-
|-
|
| <sqf>sleep 10 + random 20</sqf>
[[sleep]] 10 + [[random]] 20
| <sqf>(sleep 10) + random 20</sqf>
|
| <sqf inline>sleep 10</sqf> will return [[Nothing]], then <sqf inline>+ random 20</sqf> will be calculated but not used.<br>
([[sleep]] 10) + [[random]] 20
<sqf inline>sleep (10 + random 20)</sqf> should be used instead
| {{Inline code|[[sleep]] 10}} will return [[Nothing]], then {{Inline code|+ [[random]] 20}} will be calculated but not used.<br>
{{Inline code|[[sleep]] (10 + [[random]] 20)}} should be used instead
|}
|}
</onlyinclude>


</onlyinclude>
[[Category:Syntax]]
[[Category:Scripting Topics]]

Latest revision as of 00:52, 24 February 2023

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