short circuit evaluation

L

Lassie

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?
 
J

Jean-Marc Bourguet

Lassie said:
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.
 
V

vippstar

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;
 
R

Richard

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.
 
S

sh.vipin

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
 
K

Kenny McCormack

Richard said:
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.
 
V

vippstar

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.
 
J

jameskuyper

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 said:
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.
 
K

Keith Thompson

Richard said:
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).
 
V

vippstar

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
 
K

Keith Thompson

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>

Either that, or it was compiled with a C++ compiler.
 
B

Ben Pfaff

&& means "evaluate while true [*]; return last evaluated expression"
|| means "evaluate until true [*]; return last evaluated expression"

That is not completely correct. The result of && is always 0 or
1. It is not, say, 2, even if the last evaluated subexpression
evaluates to 2.
 
K

Kenny McCormack

[email protected] (Kenny McCormack) said:
&& means "evaluate while true [*]; return last evaluated expression"
|| means "evaluate until true [*]; return last evaluated expression"

That is not completely correct. The result of && is always 0 or
1. It is not, say, 2, even if the last evaluated subexpression
evaluates to 2.

That's true. I could have said "return the truth value of the last
evaluated expression" - but I wanted to fit it into an 80 character
line.

Of course, the phrase "truth value" probably doesn't occur in the holy
standards documents, so, no doubt, somebody will come along and nit pick
that.
 
B

Ben Pfaff

[email protected] (Kenny McCormack) said:
&& means "evaluate while true [*]; return last evaluated expression"
|| means "evaluate until true [*]; return last evaluated expression"

That is not completely correct. The result of && is always 0 or
1. It is not, say, 2, even if the last evaluated subexpression
evaluates to 2.

That's true. I could have said "return the truth value of the last
evaluated expression" - but I wanted to fit it into an 80 character
line.

I suppose you could say "return !!(last evaluated expression)" or
"return (last evaluated expression) != 0". Either way, I guess
that it is not necessarily clear to anyone who doesn't already
understand what's going on.
 
C

CBFalconer

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

Wrong. It is available in C99, provided you #include <stdbool.h>.
 
P

Peter Nilsson

CBFalconer said:
Wrong.  It is available in C99, provided you #include
<stdbool.h>.

Wrong. With or without <stdbool.h>, bool is not a built
in data type in C99.

#include <stdio.h>
#include <stdbool.h>

#undef bool

int main(void)
{
int bool = 42;
printf("%d\n", bool);
return 0;
}
 
R

Richard

Peter Nilsson said:
Wrong. With or without <stdbool.h>, bool is not a built
in data type in C99.

#include <stdio.h>
#include <stdbool.h>

#undef bool

int main(void)
{
int bool = 42;
printf("%d\n", bool);
return 0;
}

I'm not quite sure what your example is supposed to prove but my gcc
stdbool includes this comment:

/*
* ISO C Standard: 7.16 Boolean type and values <stdbool.h>
*/

And it goes on to define bool.
 
B

Ben Bacarisse

blargg said:
(e-mail address removed) (Kenny McCormack) writes:

&& means "evaluate while true [*]; return last evaluated expression"
|| means "evaluate until true [*]; return last evaluated expression"

That is not completely correct. The result of && is always 0 or
1. It is not, say, 2, even if the last evaluated subexpression
evaluates to 2.

That's true. I could have said "return the truth value of the last
evaluated expression" - but I wanted to fit it into an 80 character
line.

How about this?

a && b means a ? !!b : 0
a || b means a ? 1 : !!b

Yes, and it even has the sequence points in the right places. But (it
would not be clc without a "but" would it?) it involves even more to
learn, so it is not ideal as an explanation.

The standard gets knocked (by some) as wordy and obscure but, it does
very well here:

The && operator shall yield 1 if both of its operands compare
unequal to 0; otherwise, it yields 0. The result has type int.

Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation; there is a sequence point after the
evaluation of the first operand. If the first operand compares equal
to 0, the second operand is not evaluated.
 
K

Keith Thompson

pete said:
You can find this phrase: "These operators return values",
in the standard.

Yes, in C99 6.5p4.

This is changed to "These operators yield values" in both n1124
(C99+TC1+TC2) and n1256 (C99+TC1+TC2+TC3).

n1336.pdf, the very early first draft of C201x, also uses "yield".

Oddly, I can't find this change either in any of the Technical
Corrigenda or in any of the Defect Reports.
 
J

Joachim Schmitz

Richard said:
I'm not quite sure what your example is supposed to prove but my gcc
stdbool includes this comment:

/*
* ISO C Standard: 7.16 Boolean type and values <stdbool.h>
*/

And it goes on to define bool.

Like this:
#define bool _Bool

So it is true: C does not have a builtin type bool, C99 does have a builtin
type _Bool. C99 does have a macro bool in <stdbool.h>.

From n1256
6.2.5.2:
An onject declared as type _Bool is large enough to store the values 0 and
1.

6.3.1.1:
The rank of _Bool shall be less than the rank of all other standard integer
types.

6.7.2.1 and .2, "type specifiers" mentions _Bool, but not bool

7.16.2:
The macro bool expands to _Bool.

Bye, Jojo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top