|
|||||||||
|
|||||||||
Prerequisite Concepts | |||||||||
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:
process output "d" % 3 * 4 + 5 * 6
This program will output "42". The mathematical operations are performed in this order:
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:
process 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":
process output "d" % 12 - 3 - 6
Once again, you can use parentheses to change the order of operations:
process 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.
Prerequisite Concepts Arithmetic and comparison operators |
---- |