Next: Cost of Arithmetic Operators
Up: Simple Input / Output
Previous: Operator Precedence
The precedence is worked out as follows
- in an expression find the operator(s) with the tightest binding.
- if there are more than one occurrence of this operator then the
separate instances are evaluated left to right.
- place the first executed subexpression in brackets to signify
this.
- continue with the second and subsequent subexpressions.
- move to next most tightest binding operator and follow the same
procedure.
It is easy to make mistakes by forgetting the implications of
precedence. The following expression,
x = a+b/5.0-c**d+1*e
is equivalent to
x = ((a+(b/5.0))-(c**d))+1*e
The following procedure has been followed to parenthesise the expression,
- tightest binding operator is **. This means c**d
is the first executed subexpression so should be surrounded by brackets.
- / and * are the second most tightly binding operators
and expressions
involving them will be evaluated next, put b/5.0 and 1*e
in brackets.
- + and - have the next highest precedence. Since they are
of equal precedence, occurrences are evaluated from left
- at last the assignment is made.
Likewise, the following expression,
.NOT.A.OR.B.EQV.C.AND.D.OR..NOT.E
is equivalent to
((.NOT.A).OR.B).EQV.((C.AND.D).OR.(.NOT.E))
here,
- the tightest binding operator is: .NOT. followed by .AND.
followed
by .OR..
- the two subexpressions containing the monadic .NOT. are evaluated
first, as there are two of these the leftmost, .NOT.A is done
first followed by .NOT.E.
- the subexpression C.AND.D is evaluated next followed by
.OR.
(left to right)
- finally the .EQV. is executed
Parentheses can be added to any expression to modify the order of
evaluation.
Now try this question
Return to corresponding overview page
Next: Cost of Arithmetic Operators
Up: Simple Input / Output
Previous: Operator Precedence
©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997Not for commercial use.