Pete Tyo

Ranch Hand

Posts: 38

posted 12 years ago

I am confused on how to evaluate a Boolean expression. Take for example

P && Q || !P && !q

Okay, I was under the impression ! has the higher precedence so I would evaluate (!p & !q) first. After asking my professor he said I would evaluate P && Q first if they evaluate to True then the compiler doesn't go any further (short circuit). So what exactlly is the precedence have to do with anything?

P && Q || !P && !q

Okay, I was under the impression ! has the higher precedence so I would evaluate (!p & !q) first. After asking my professor he said I would evaluate P && Q first if they evaluate to True then the compiler doesn't go any further (short circuit). So what exactlly is the precedence have to do with anything?

Nick George

Ranch Hand

Posts: 815

posted 12 years ago

So, short ciruiting means that as soon as the program knows that an expression is true or false, it acts, as opposed to checking every single term.

For instance, if we have the statement p || q, and we find p to be true, we don't care what q is, because the statement is automatically true. Similarly, if we have p && q, and we know p to be false, we don't look at q.

so, in your example of

P && Q || !P && !Q

we first look at the left side of the big or. If it is true, we short circuit. Now, in evaluating that left side, we follow the order of operations. So if our big statement was

!P && Q || !P && !Q,

we first check the left side, which is not-p AND q, because not comes before and. If and came first, the left side would be not (p AND q)

dig?

For instance, if we have the statement p || q, and we find p to be true, we don't care what q is, because the statement is automatically true. Similarly, if we have p && q, and we know p to be false, we don't look at q.

so, in your example of

P && Q || !P && !Q

we first look at the left side of the big or. If it is true, we short circuit. Now, in evaluating that left side, we follow the order of operations. So if our big statement was

!P && Q || !P && !Q,

we first check the left side, which is not-p AND q, because not comes before and. If and came first, the left side would be not (p AND q)

dig?

I've heard it takes forever to grow a woman from the ground

Pete Tyo

Ranch Hand

Posts: 38

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 12 years ago

The order of precedence stuff is tough to remember and I try not to rely on it. If I have to do anything tricky I use parens to make the order explicit.

&& is higher than || so your test is:

( P && Q ) || ( !P && !Q )

The ! has higher precedence yet which binds it to the following P or Q. If we didn't know that we might wonder if your expression meant:

!(P && !Q) --- or --- (!P) && (!Q)

For fun, see if these give the same result:

( P == Q )

!( P ^ Q )

&& is higher than || so your test is:

( P && Q ) || ( !P && !Q )

The ! has higher precedence yet which binds it to the following P or Q. If we didn't know that we might wonder if your expression meant:

!(P && !Q) --- or --- (!P) && (!Q)

For fun, see if these give the same result:

( P == Q )

!( P ^ Q )

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Consider Paul's rocket mass heater. |