Order of Precedence: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "[[config / name" to "[[a / b")
m (Some wiki formatting)
Line 124: Line 124:
! Comment
! Comment
|-
|-
|
| <sqf>1 + 2 * 3</sqf>
1 + 2 * 3
|
|
  1 + (2 * 3)
  1 + (2 * 3)
| result equals 7, and not 9 (see also {{Wikipedia|Order of operations|PEMDAS}})
| 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
| {{ic|[[sleep]] 10}} will return [[Nothing]], then {{ic|+ [[random]] 20}} will be calculated but not used.<br>
{{ic|[[sleep]] (10 + [[random]] 20)}} should be used instead
|}
|}


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

Revision as of 12:19, 22 July 2022

Introduction

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