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"