Re: IF statement, I don't understand this



On Thu, 30 Mar 2006 08:03:25 -0800, "Chuck Stevens"
<charles.stevens@xxxxxxxxxx> wrote:

Another mistaken assumption is that abbreviated combined relation conditions
somehow imply a different (and probably higher) precedence from that which
would occur in their fully-expanded counterparts -- that the fact that an
abbreviation occurs in 'ASM010-I-DIR = "L" OR "R" OR "S" ' somehow means
this gets evaluated as a unit (as it might in English). It doesn't. The
abbreviated conditions are treated as if they were expressed in their full
form, and the resulting full conditional expression is then evaluated.
Absent parentheses, it's AND before OR, and otherwise left to right.

Agreed. I never use them for that reason, and because I prefer
writing my conditions one per line (which makes commenting them easy).

e.g.
IF (SHH-SHIP-CODE = 5
AND ASM010-I-DIR = "L" )
OR ASM010-I-DIR = "R"
OR ASM010-I-DIR = "S"
or even
IF (
SHH-SHIP-CODE = 5
AND ASM010-I-DIR = "L"
)
OR ASM010-I-DIR = "R"
OR ASM010-I-DIR = "S"

With a compiler that accepts in-line comments, this method has another
advantage - we can say what "R" means!
.



Relevant Pages

  • Re: Conditional expressions - PEP 308
    ... The conditional expression is defined as X if C else Y. ... We don't know the precedence of the "if" operator. ... print 'res1:', res ... I was trying to make the point that the parentheses are necessary if X is more than a simple value. ...
    (comp.lang.python)
  • Re: Conditional expressions - PEP 308
    ... The conditional expression is defined as X if C else Y. ... We don't know the precedence of the "if" operator. ... print 'res1:', res ... if-expression, and the example given consistes of just a simple ...
    (comp.lang.python)
  • Re: Conditional expressions - PEP 308
    ... Colin J. Williams wrote: ... it seem to have a lower precedence than "or". ... it is desirable for the user to put the conditional expression in parentheses. ...
    (comp.lang.python)