Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site mit-eddie.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxn!ihnp4!mit-eddie!barmar From: barmar@mit-eddie.UUCP (Barry Margolin) Newsgroups: net.lang Subject: Re: Operator Precedence Message-ID: <5257@mit-eddie.UUCP> Date: Sun, 8-Sep-85 04:33:37 EDT Article-I.D.: mit-eddi.5257 Posted: Sun Sep 8 04:33:37 1985 Date-Received: Mon, 9-Sep-85 04:32:44 EDT References: <262@pedsgd.UUCP> Reply-To: barmar@mit-eddie.UUCP (Barry Margolin) Organization: MIT, Cambridge, MA Lines: 52 In article <262@pedsgd.UUCP> bob@pedsgd.UUCP (Robert A. Weiler) writes: > Well, we have Fortran to thank for operator precedence in programming languages, I suspect. As for how it got there in the first place, here is my hypothesis. It is an attempt to emulate the precedence inherent in mathematical notation. In mathematics, one does not normally write the multiplication operator, one writes A*B as AB. So, when one sees the expression A+BC in mathematics, it is implicitly parsed as A+(BC). A similar argument accounts for the higher precedence of exponentiation (** or ^ in most Fortran-descendents): N is generally interpreted as AB N A(B ). As for the higher precedence of division, that may be because of N fractions being written as N/D when - is not convenient, or it might be D because division is to subtraction as multiplication is to addition, so the corresponding operations have similar precedence. Also, division is almost associative with multiplication, i.e. A*B/C can be interpreted as (A*B)/C or A*(B/C) without affecting the mathematical answer (although the computer answers might be different, due to the problems of computer representations of numbers). The "founding fathers" of high-level languages probably didn't anticipate the confusion that would ensue when their simple rule of operator precedence (* and / higher than + and - [= was not considered part of an expression, it was part of the syntax of an assignment statement, and the original Fortran didn't even have comparison operators (remember arithmetical IF?)]) got extended to handle a dozens of operators. Even after comparison operators came around, their results could usually be used only in boolean expresions, and A+(B==C) is not valid in many languages, so it was very natural that A+B==C should be forced to mean (A+B)==C. The only operator precedence I can ever remember is that ^ is higher than * and /, which are higher than + and -, and that comparison operators are the lowest. And, in the case of languages in which assignments are expressions (are there any other commonly-used ones besides C [not counting functional languages, in which everything is an expression by definition]?), assignments are usually even lower. These last two are easy to remember because they match the common use: one usually uses comparisons and assignments at the highest possible levels, rather than using them as subexpresions; the C expression a*b==c+d almost always is intended to compare the two outer expressions, rather than to involve the result of the inner comparison in the arithmetic expression. -- Barry Margolin ARPA: barmar@MIT-Multics UUCP: ..!genrad!mit-eddie!barmar