Next: Control Constructs
Up: Expressions and Assignment
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
is equivalent to
x = ((a+(b/5.0))-(c**d))+1
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.
- / is the second most tightly binding operator and expressions
involving it will be evaluated next, put b/5.0 in brackets.
- + and - have the next highest precedence. Since they are
of equal precedence, occurrences are evaluated from left
to right, in other words a+(b/5.0) is evaluated first followed by
(a+(b/5.0))-(c**d) followed by ((a+(b/5.0))-(c**d))+1
- 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
Next: Control Constructs
Up: Expressions and Assignment
Previous: Operator Precedence
Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 15:03:35 GMT 1996Not for commercial use.