Article View: comp.lang.c.moderated
Article #2459Re: Explaining precedence vs. sequence
From: Lawrence Kirby
Date: Tue, 02 Apr 1996 01:55
Date: Tue, 02 Apr 1996 01:55
83 lines
3647 bytes
3647 bytes
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.
In the expression && has 2 operands:
LHS: GetState (value)
RHS: dumb == !smart
|| evaluates its RHS only if the LHS evaluates to non-zero.
>I'm not
>positive on this, but I suspect that a logical operator imposes a
>sequence point that forces the evaluation of the left side. (Please
>confirm if this is true.)
&& and || always evaluate their LHS operands. Of course this will only happen
if they are themselves evaluated. In the example && won't be evaluated if
|| short-circuits its RHS.
>A few
>people in the class pointed out that the parenthetical nature of the
>expression on the right of the || should force it to be evaluated
>first.
Parentheses simply force grouping in C. Beyond the implications of that
they have no effect on order of execution.
> I know that the standard says that logical operators guarantee
>that the evaluation must be performed left to right and stops when the
>expression reaches a guaranteed true or a guaranteed false.
That's an oversimplification. && and || guarantee that their LHS is
evaluated before their RHS is (possibly) evaluated and that there is a
sequence point after the LHS is evaluated. Consider:
if ((p1 && p2) || (p3 && p4))
If p1 evaluates to zero then p2 will not be evaluated. However p3 will
be evaluated and p4 may be evaluated. Hence left to right evaluation
is only applicable to a single operator and its operands not to the
expression as a whole. A question for you: what are the possible
orders of evaluation of:
if ((p1 && p2) | (p3 && p4))
(note single |) or, if you prefer, what are the constraints on the order of
evaluation?
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Technical Details
Message-ID:
<4jq1fe$a48@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4ja3pu$p8r@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Lawrence Kirby <fred@genesis.demon.co.uk> Newsgroups: comp.lang.c.moderated Subject: Re: Explaining precedence vs. sequence Date: 1 Apr 1996 19:55:58 -0600 Organization: none Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jq1fe$a48@solutions.solon.com> References: <4ja3pu$p8r@solutions.solon.com> Reply-To: fred@genesis.demon.co.uk NNTP-Posting-Host: solutions.solon.com X-NNTP-Posting-Host: genesis.demon.co.uk X-Newsreader: Demon Internet Simple News v1.27 X-Mail2News-Path: disperse.demon.co.uk!post.demon.co.uk!genesis.demon.co.uk