Operator precedence

Arithmetical operators in VBA, as in everyday arithmetic, are not evaluated from left to right, but follow rules of precedence. These prevent ambiguity regarding the order in which arithmetical operations are to be followed. These rules of precedence can be memorised from the BODMAS mnemonic (Brackets, power Of, Division, Multiplication, Addition, Subtraction).

For instance, the expression 9 * 4 - 3 would give the result = 33, since multiplication (*) has a higher precedence than subtraction (-). Thus, 9 * 4 would be evaluated first giving 36, followed by 36 - 3 giving 33. On the other hand, the expression 9 * (4 - 3) would give the result = 9, as brackets have a higher precedence than multiplication. Thus, here the (4 - 3 ) part is evaluated first, giving 1 followed by multiplication by 9.

0 0

Responses

Post a comment