Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #28333

Re: verifying precedence

From Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: verifying precedence
Date 2014-05-01 10:35 -0500
Organization A noiseless patient Spider
Message-ID <ljtpkp$isn$1@dont-email.me> (permalink)
References <ivc4m95b309smq7g0pkes96cfpmdfsmj6q@4ax.com> <AfOdnQTUOv31pf_OnZ2dnUVZ8tednZ2d@giganews.com>

Show all headers | View raw


On 5/1/2014 7:04 AM, Leif Roar Moldskred wrote:
> Roedy Green <see_website@mindprod.com.invalid> wrote:
>>
>> You could consult a precedence table, but you could misinterpret it.
>> Java's rules of precedence and association are quite complicated.
>>
>
> "Practical Programming in C" summed up the precedence rules in C
> thusly:
>
> " Precedence       Operator
>        1.            * / %
>        2.            + -
>
>    Put parantheses around everything else."
>
> If the meaning of your code depends on the particulars of operator
> precedence beyond the very obvious rules above, rewrite it so that it
> doesn't. (The same holds for the difference between ++i and i++.)

That's not quite the right rules, but probably because it's ignoring 
half the operators in the first place. I'd remember it as:
1. Unary operators [includes parentheses]
2. * / %
3. + -
4. Relational (<, >, ==, etc.)
5. Logical (&&, ||)
6. ?:
7. = and op=

The position of shift and bitwise operators is the part I can never 
remember: C (by accident of history [1]) put the bitwise below 
relational operators instead of above it (so you have to say (x & mask) 
== mask instead of x & mask == mask). Some descendants fixed the issue, 
while others did not, so as someone who regularly uses several different 
languages, I can never remember if the language I'm in fixed the 
precedence or not.

Note that the table I give glides over the binding of a few operators 
(x++ and ++x are different levels, as are < and ==), but in terms of 
remembering where I need to put parentheses, it's sufficient.


[1] Logical, short-circuit and and or came much later. That's why the 
and pseudo-keyword maps to &, not &&. Arguably, when the logical 
operators had been introduced, the bitwise operators ought to have been 
moved in precedence, but maybe the authors of C didn't want to risk such 
a breaking change at that time.

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

verifying precedence Roedy Green <see_website@mindprod.com.invalid> - 2014-05-01 04:50 -0700
  Re: verifying precedence leif.roar@invalid.com (Leif Roar Moldskred) - 2014-05-01 07:04 -0500
    Re: verifying precedence Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-05-01 10:35 -0500
      Re: verifying precedence Joerg Meier <joergmmeier@arcor.de> - 2014-05-01 17:40 +0200
        Re: verifying precedence Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-05-01 10:49 -0500
          Re: verifying precedence Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-05-01 17:05 -0500
            Re: verifying precedence Gene Wirchenko <genew@telus.net> - 2014-05-02 10:22 -0700
            Re: verifying precedence "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-05-04 10:53 +0100
              Re: verifying precedence Simon Lewis <simonlewis2001@gmail.com> - 2014-05-04 13:23 +0200
          Re: verifying precedence Arne VajhĂžj <arne@vajhoej.dk> - 2014-05-01 18:51 -0400
          Re: verifying precedence Arne VajhĂžj <arne@vajhoej.dk> - 2014-05-01 19:00 -0400
          Re: verifying precedence Roedy Green <see_website@mindprod.com.invalid> - 2014-05-01 17:48 -0700
          Re: verifying precedence Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-05-02 13:29 -0400
      Re: verifying precedence Arne VajhĂžj <arne@vajhoej.dk> - 2014-05-01 18:50 -0400
        Re: verifying precedence Arne VajhĂžj <arne@vajhoej.dk> - 2014-05-01 19:18 -0400
    Re: verifying precedence Roedy Green <see_website@mindprod.com.invalid> - 2014-05-01 17:46 -0700
      Re: verifying precedence Arne VajhĂžj <arne@vajhoej.dk> - 2014-05-01 21:27 -0400

csiph-web