Article View: comp.lang.c.moderated
Article #2486Re: Explaining precedence vs. sequence
From: Kazimir Kylheku
Date: Wed, 03 Apr 1996 13:28
Date: Wed, 03 Apr 1996 13:28
53 lines
2466 bytes
2466 bytes
In article <4jq1fe$a48@solutions.solon.com>,
Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
>In article <4ja3pu$p8r@solutions.solon.com>
> bcalbridge@dcccd.edu "Bob Calbridge" writes:
>
>>I'm currently taking a C++ class in order to teach it next Fall.
>>I've taught C programming for the last three years so I know a bit
>>more than some of the people who program in it that they bring in to
>>teach it. The class I'm in got into a discussion that I, as a fellow
>>"student" had a hard time explaining to the class.
>>
>>The issue revolves around an if statement with a predicate that may
>>look like the following:
>>
>> if (value || ( GetState (value) && dumb == !smart)) {.....
>
>Those other explanations looked rather complex and involved (well at least
>to me!) ;-)
>
>In the example expression || has 2 operands:
>
> LHS: value
>
> RHS: ( GetState (value) && dumb == !smart))
>
>|| evaluates its RHS only if the LHS evaluates to zero.
>
>>The instructor agrees with me, as I'm sure everyone else here would,
>>that if value evaluates to true the function GetState() will never
>>execute due to short circuiting of the logical operators.
>
>Right, except that it is the whole sub-expression that forms the RHS operand
>that is short-circuited.
I think that the whole problem here is a confusion between parsing (syntax) and
valuation order (semantics), which can be ``decoupled''.
The evaluation order in this case is partly bottom-up (the values are
synthesized starting from leaf nodes, working their way up), but partly
dictated by semantics that work top-down.
If you allow, I will write a piece of pseudo code to simulate:
function eval (tree T) : integer
if T.operator = '||' then
if (!eval(T.left)) then
return eval(T.right)
else
return TRUE;
endif
elsif T.operator = '*' then
return eval(T.right) * eval(T.left);
endif
Technical Details
Message-ID:
<4jtueq$3vp@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4ja3pu$p8r@solutions.solon.com> <4jq1fe$a48@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) Newsgroups: comp.lang.c.moderated Subject: Re: Explaining precedence vs. sequence Date: 3 Apr 1996 07:28:58 -0600 Organization: Computer Science, University of B.C., Vancouver, B.C., Canada Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jtueq$3vp@solutions.solon.com> References: <4ja3pu$p8r@solutions.solon.com> <4jq1fe$a48@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com