Precedence and order of operations

In a complex expression, operations are performed according to their order of precedence. For instance, consider the mathematical expression in the following program:

     output "d" % 3 * 4 + 5 * 6

This program will output "42". The mathematical operations are performed in this order:

  • 3 * 4 (yielding 12)
  • 5 * 6 (yielding 30)
  • 12 + 30

Had the operations been performed in written order, left to right, the result would have been 102. You can force this order of evaluation using parentheses:

     output "d" % ((3 * 4) + 5) * 6

Any operation contained in parentheses is performed first. In the case of nested parentheses, the most deeply nested operations are performed first.

Operations of the same precedence level are performed in order from left to right. Thus the following program outputs "3":

     output "d" % 12 - 3 - 6

Once again, you can use parentheses to change the order of operations:

     output "d" % 12 - (3 - 6)

The code above outputs "15".

The following table gives the precedence for OmniMark operators. Items at the top of the list have low precedence. That is, they are performed last. Items at the bottom of the list have high precedence and are performed first.

Note that if you overload an OmniMark operator, that operator retains its precedence order. However, if you write a function with the name of an OmniMark operator that is not overloadable, your function will override the OmniMark operator (even if your function is written as overloaded) and your function will fall into the "everything else" precedence category, despite the precedence of the name in this table.

Prerequisite Concepts