Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > short circuit evaluation

Reply
Thread Tools

short circuit evaluation

 
 
Lassie
Guest
Posts: n/a
 
      09-16-2008
bool finished = is_done();

if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?

 
Reply With Quote
 
 
 
 
Jean-Marc Bourguet
Guest
Posts: n/a
 
      09-16-2008
"Lassie" <(E-Mail Removed)> writes:

> if (finished && try_again()) { }
>
> Is my understanding of short circuit evaluation correct that if finished
> is false that try_again() will never be executed?


Yes.

--
Jean-Marc
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-16-2008
On Sep 16, 6:55 pm, "Lassie" <(E-Mail Removed)> wrote:
> bool finished = is_done();
>
> if (finished && try_again()) { }
>
> Is my understanding of short circuit evaluation correct that if finished is
> false that try_again() will never be executed?
> Or does it get evaluated sometimes?



No, it does not evaluate if finished is 0.

Thus, this is correct:

return 0 && 1 / 0;

The expression (0 && 1 / 0) is evaluated like this:

(a && b)

First, a is evaluated. If a is false, then the whole expression is
replaced by 0. (false)
If a is true, then b is evaluated, and the expression is replaced by
true or false, depending on what b was evaluated.

This is also correct

return 1 || 1 / 0;

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-16-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> On Sep 16, 6:55 pm, "Lassie" <(E-Mail Removed)> wrote:
>> bool finished = is_done();
>>
>> if (finished && try_again()) { }
>>
>> Is my understanding of short circuit evaluation correct that if finished is
>> false that try_again() will never be executed?
>> Or does it get evaluated sometimes?

>
>
> No, it does not evaluate if finished is 0.
>
> Thus, this is correct:
>
> return 0 && 1 / 0;
>
> The expression (0 && 1 / 0) is evaluated like this:
>
> (a && b)
>
> First, a is evaluated. If a is false, then the whole expression is
> replaced by 0. (false)
> If a is true, then b is evaluated, and the expression is replaced by
> true or false, depending on what b was evaluated.
>
> This is also correct
>
> return 1 || 1 / 0;
>


Wow what a long winded way of explaining something.

Short answer : as soon as one "false" is matched from left to right the
&& statement "exits" and returns false.
 
Reply With Quote
 
sh.vipin@gmail.com
Guest
Posts: n/a
 
      09-16-2008
On Sep 16, 8:55*pm, "Lassie" <(E-Mail Removed)> wrote:
> bool finished = is_done();
>
> if (finished && try_again()) { }
>
> Is my understanding of short circuit evaluation correct that if finished is
> false that try_again() will never be executed?
> Or does it get evaluated sometimes?


no it will not be evaluated ever,
but are you trying it in "C".
In "C" there is no "bool" built in data type .
If you type casted "bool" as some other built in data type then please
post as much relevant code as possible in queries in future

--
vIpIn
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      09-16-2008
In article <gaom77$gm1$(E-Mail Removed)>,
Richard <(E-Mail Removed)> wrote in regards to our friend vippy:
....
>Wow what a long winded way of explaining something.


That is the way - if you are a reg wannabee.

>Short answer : as soon as one "false" is matched from left to right the
>&& statement "exits" and returns false.


Another way:
&& means "evaluate while true[*]; return last evaluated expression"
|| means "evaluate until true[*]; return last evaluated expression"
[*] Or end of list reached.

 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-16-2008
On Sep 16, 7:23 pm, (E-Mail Removed) wrote:
> On Sep 16, 8:55 pm, "Lassie" <(E-Mail Removed)> wrote:
>
> > bool finished = is_done();

>
> > if (finished && try_again()) { }

>
> > Is my understanding of short circuit evaluation correct that if finished is
> > false that try_again() will never be executed?
> > Or does it get evaluated sometimes?

>
> no it will not be evaluated ever,
> but are you trying it in "C".
> In "C" there is no "bool" built in data type .
> If you type casted "bool" as some other built in data type then please
> post as much relevant code as possible in queries in future


There's a bool macro in <stdbool.h> in C99.
 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      09-16-2008
(E-Mail Removed) wrote:
> On Sep 16, 8:55�pm, "Lassie" <(E-Mail Removed)> wrote:
> > bool finished = is_done();
> >
> > if (finished && try_again()) { }
> >
> > Is my understanding of short circuit evaluation correct that if finished is
> > false that try_again() will never be executed?
> > Or does it get evaluated sometimes?

>
> no it will not be evaluated ever,
> but are you trying it in "C".
> In "C" there is no "bool" built in data type .


No, but it is a standard typedef. This is just a code fragment, which
doesn't even provide declarations for is_done() and try_again(). I
would assume that the missing code also contains a line which says

#include <stdbool.h>

> If you type casted "bool" as some other built in data type then please
> post as much relevant code as possible in queries in future


Agreed: explaining a problem by providing a complete program that
demonstrates it always makes for more productive responses.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-16-2008
Richard<(E-Mail Removed)> writes:
> (E-Mail Removed) writes:
>
>> On Sep 16, 6:55 pm, "Lassie" <(E-Mail Removed)> wrote:
>>> bool finished = is_done();
>>>
>>> if (finished && try_again()) { }
>>>
>>> Is my understanding of short circuit evaluation correct that if finished is
>>> false that try_again() will never be executed?
>>> Or does it get evaluated sometimes?

>>
>>
>> No, it does not evaluate if finished is 0.
>>
>> Thus, this is correct:
>>
>> return 0 && 1 / 0;
>>
>> The expression (0 && 1 / 0) is evaluated like this:
>>
>> (a && b)
>>
>> First, a is evaluated. If a is false, then the whole expression is
>> replaced by 0. (false)
>> If a is true, then b is evaluated, and the expression is replaced by
>> true or false, depending on what b was evaluated.
>>
>> This is also correct
>>
>> return 1 || 1 / 0;
>>

>
> Wow what a long winded way of explaining something.
>
> Short answer : as soon as one "false" is matched from left to right the
> && statement "exits" and returns false.


It's helpful to use correct terminology.

&& is not a statement, it's an operator, and it can be part of an
expression. Since it's not a function it doesn't "return"
anything; expressions *yield* values (though it's common to say that
expressions return values, and it usually doesn't cause too much
confusion). Your wording could be taken to imply that a && b && c is
evaluated as if it were an && expression with 3 operands; in a sense
it works that way, but it's actually built up from two 2-operand
&& subexpressions, equivalent to (a && b) && c. It's important
to understand how expressions are built up from subexpressions;
failing to understand this can easily lead to errors like
0 <= x <= 100 (which is valid, but doesn't mean what one might
assume). (Richard, I'm sure you know what 0 <= x <= 100 means.)

So ...

a && b yields a true value (1) if both of its operands are true
(non-zero); otherwise it yields a false value (0). If the first
operand is false, the second operand is not evaluated. The two
operands are evaluated in sequence; if the second operand is
evaluated this happens *after* the first operand is evaluated.

a || b yields a true value (1) if either of its operands is true
(non-zero); otherwise it yields a false value (0). If the first
operand is true, the second operand is not evaluated. The two
operands are evaluated in sequence; if the second operand is
evaluated this happens *after* the first operand is evaluated.

As with 2-operand operator, && expressions can be chained together,
as in a && b && c && d, which is equivalent to ((a && b) && c) && d.
This yields 1 if all of a, b, c, or d are true; otherwise it
yields 0. a, b, c, and d are evaluated in strict left-to-right
order; if any of them is false, the rest are not evaluated.
|| can be chained similarly. There are not special-case rules;
they follow from the rules for && and || (including the fact that
they're left-associative).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-16-2008
On Sep 16, 7:38 pm, (E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
> > On Sep 16, 8:55 pm, "Lassie" <(E-Mail Removed)> wrote:
> > > bool finished = is_done();

>
> > > if (finished && try_again()) { }

>
> > > Is my understanding of short circuit evaluation correct that if finished is
> > > false that try_again() will never be executed?
> > > Or does it get evaluated sometimes?

>
> > no it will not be evaluated ever,
> > but are you trying it in "C".
> > In "C" there is no "bool" built in data type .

>
> No, but it is a standard typedef. This is just a code fragment, which
> doesn't even provide declarations for is_done() and try_again(). I
> would assume that the missing code also contains a line which says
>
> #include <stdbool.h>


It is actually a macro, it's explicity mentioned in the standard to be
so.
See 7.16 p 2
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
logical expressions simplification with short circuit evaluation mingze zhang C Programming 4 10-22-2011 09:51 AM
logic expressions & short circuit evaluation mingze zhang C++ 2 07-15-2010 01:56 PM
short-circuit evaluation and assignment operators Anthony Paul C Programming 5 06-06-2009 06:49 PM
Short-Circuit Evaluation of Boolean Expressions webposter C Programming 2 09-14-2004 03:43 AM
short-circuit operators - part of the spec? Phil... Java 10 10-23-2003 08:42 PM



Advertisments