The Sway Reference Manual/Precedence and Associativity
Precedence[edit | edit source]
Precedence (partially) describes the order in which operators, in an expression involving different operators, are evaluated. In Sway, the expression
3 + 4 < 10 - 2
evaluates to true. In particular, 3 + 4 and 10 - 2 are evaluated before the <, yielding 7 < 8, which is indeed true. This implies that + and - have higher precedence than <. If < had higher precedence, then 4 < 10 would be evaluated first, yielding 3 + true - 2, which is nonsensical.
Note that precedence is only a partial ordering. We cannot tell, for example whether 3 + 4 is evaluated before the 10 - 2, or vice versa. Upon close examination, we see that it does not matter which is performed first as long as both are performed before the expression involving < is evaluated. It is common to assume that the left operand is evaluated before the right operand. For the logical connectives, this is indeed true. But for other operators, such an assumption can lead you into trouble. You will learn why later. For now, remember never, never, never depend on the order in which operands are evaluated!
The lowest precedence operator in Sway is the assignment operator which is described later. Next come the Boolean connectives && and ||. At the next higher level are the Boolean comparatives, <, <=, >, >=, ==, and !=. After that come the arithmetic operators +, -, *, /, and %. Finally, at the highest level of precedence is the selection, or dot operator (the dot operator is a period or full-stop). Higher precedence operations are performed before lower precedence operations. Functions which are called with operator syntax have the same precedence level as the mathematical operators.
Unlike the algebraic precedence you learned in grammar school, multiplication and division are at the same precedence level as addition and subtraction in Sway. Thus, in combinations of these operators, operations to the left are performed before operations to the right. To force multiplication and division to be performed prior to addition and subtraction, parentheses must be used. For example:
sway> 5 + 2 * 3 //5 plus 2 is 7, 7 times 3 is 21 INTEGER: 21
sway> 5 + (2 * 3) //2 * 3 is 6, 5 + 6 is 11 INTEGER: 11
Associativity[edit | edit source]
Associativity describes how multiple expressions connected by operators at the same precedence level are evaluated. All the operators, with the exception of the assignment operator, are left associative. For example the expression 5 - 4 - 3 - 2 - 1 is equivalent to ((((5 - 4) - 3) - 2) - 1). For a left-associative structure, the equivalent, fully parenthesized, structure has open parentheses piling up on the left. If the minus operator was right associative, the equivalent expression would be (5 - (4 - (3 - (2 - 1)))), with the close parentheses piling up on the right. For a commutative operator, it does not matter whether it is left associative or right associative. Subtraction, however, is not commutative, so associativity does matter. For the given expression, the left associative evaluation is -5. If minus were right associative, the evaluation would be 3. The only operator that is right associate is the assignment operator. You will see why in the assignment chapter.