MCP
MCP

Reputation: 4536

true/false precedence in bool recursive function

I've done some reading of previous posts and I've learned some things but want to verify how some loops are working. In reading, am I right in understanding that "true" has higher precedence than "false"? For instance:

/.../
return (true || false);

will return "true" (regardless of order)?

If I have a boolean recursive function that calls 3 variations of itself...all I need is for one version to return true for the whole funct to return true, correct? The function below creates it's stack frame, then the return call creates 3 more stack frames and runs through the calls, then if one returns true the whole funct returns true because true has precedence over false... Is this assumption correct?

Ie:

/* This function is taking a given weight and seeing if it can be offset by available 
 * weights. Depending on what weights are available, the weights can be directly opposed
 * to "weight" (opposite side of scale) or added to... The recursive calls will either all 
 * return false, all return true, or a variation thereof.  All that I can is that if one 
 * branch returns true, the overall function returns true...
*/

bool CanMeasure(int weight, std::vector<int> &availableWeights, int index = 0)
{
    /.../
    // the below can return all true, all false, or some variation thereof...
    return (CanMeasure(weight + availableWeights[index], availableWeights, index + 1) ||
            CanMeasure(weight - availableWeights[index], availableWeights, index + 1) ||
            CanMeasure(weight, availableWeights, index + 1));
}

Thanks guys!

Upvotes: 1

Views: 1205

Answers (6)

bartgol
bartgol

Reputation: 1873

I don't think this question is about short circuit evaluation. And "true has higher precedence than false" does not make sense. True is a value, not an operation.

The reason why true || false returns true is just logic, and has nothing to do here with programming. The logic operation AND and OR don't care about the order of their inputs. They just care about "how many are true". In particular, AND returns true if both are true, while OR returns true if at least one is true.

The short circuit evaluation is another topic (which might still be useful to you to avoid some function call though).

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

Yes (it will return true regardless of order). The conditions in the or are evaluated from left to right, and when the first true is stumbled upon, the whole condition returns true.

In your example:

return (CanMeasure(weight + availableWeights[index], availableWeights, index + 1) ||
        CanMeasure(weight - availableWeights[index], availableWeights, index + 1) ||
        CanMeasure(weight, availableWeights, index + 1));

not all conditions are necesarily evaluated. If the first one evaluates to true, the others will not execute, and the function will just return true.

It's called short-circuiting.

Let's take a look at some dissasembled code:

   if ( foo() || goo() )
0041152E  call        foo (41111Dh) 
00411533  movzx       eax,al 
00411536  test        eax,eax 
00411538  jne         wmain+36h (411546h) 
0041153A  call        goo (4111A9h) 
0041153F  movzx       eax,al 
00411542  test        eax,eax 
00411544  je          wmain+49h (411559h) 

In this example, foo() and goo() are both functions returning bool.

The instruction

00411538  jne         wmain+36h (411546h) 

tells the runtime to jump out of the conditional if foo() evaluated to true.

This code is not optimized, so it's not an optimization feature.

Upvotes: 3

Sam
Sam

Reputation: 7858

Yes, one return value, which is true does suffice to determine the result. This is defined by short circuit evaluation.

http://en.wikipedia.org/wiki/Short-circuit_evaluation

Upvotes: 1

Lucero
Lucero

Reputation: 60190

true and false are values, not operators - as such they have no precedence.

However, the && and || operators do shortcut the evaluation if the result is known; so if the lefthand expression yields true and you apply ||, the righthand expression will not be evaluated; the same applies to false and &&.

Upvotes: 6

gabitzish
gabitzish

Reputation: 9691

The rules for || operation:

true || true => true
true || false => true
false || true => true
false || false => false

If one of the operands are true, no matter of it's position, the result will be true. Even if you have something like false || false || false ... false || true the result will be true.

Upvotes: 3

StuartLC
StuartLC

Reputation: 107277

The reason why return (true || false); doesn't evaluate the false bit is because of short circuit boolean evaluation. See Is Short Circuit Evaluation guaranteed In C++ as it is in Java?. Similary with &&, (false && true) won't evaluate 'true'.

Upvotes: 4

Related Questions