Jump to content

Rexx Programming/How to Rexx/precedence

From Wikibooks, open books for an open world

Rules of precedence are used to determine the order in which operators within an expression are used during evaluation. An inexperienced programmer may expect the expression from the following program to produce a result of 35. However, this actually produces a value of 23, because multiplication has a higher precedence than addition, so it is performed first.

/* This does not produce a value of 23 */ say 3 + 4 * 5

Using parentheses to control the order of evaluation

[edit | edit source]

It is possible to change the order in which operators within an expression are evaluated by using parentheses. In the following example, the expression produces a value of 35, because the parentheses have a higher precedence than multiplication, so their contents are evaluated first:

/* Parentheses cause the addition to be evaluated before the multiplication */
say (3 + 4) * 5

Groups of operators or functions within parentheses have the same precedence as normal

[edit | edit source]

Groups of operators or functions within parentheses have the same precedence as they normal would. In the following example, the multiplication within the parentheses occurs before the addition:

/* The multiplication within the parentheses is evaluated before the addition */
say 3 * (4 + 2 * 3)

Operators of equal precedence are evaluated from left to right

[edit | edit source]
say 5 - 2 + 1