# if and else statements rogether with propositionlogic

• Jul 6th 2011, 11:50 PM
iwan1981
if and else statements rogether with propositionlogic
Hi,

I am looking at this question:

Consider the following program:
if X then RED else if Y then BLUE else if Z then YELLOW else GREEN.
Under which of the following conditions is GREEN executed?

With what proposition will GREEN only be executed here?

¬
X¬Y ¬Z
¬(XY Z)
¬X¬Y ¬Z
¬(XY Z)

If I write this out in code I get:
Code:

```                  if X then       RED   elseif Y then       BLUE elseif Z then       YELLOW else       GREEN   end if```
So with the first one ¬X¬Y ¬Z
Means "NOT X AND NOT Y AND NOT Z"
So the only conclusion can be NOT RED NOT BLUE NOT YELLOW BUT GREEN.
This is correct right?

But the second one confuses me with the brackets ...
What are the rules regarding that?
• Jul 7th 2011, 02:55 AM
emakarov
Re: if and else statements rogether with propositionlogic
Quote:

But the second one confuses me with the brackets ...
What are the rules regarding that?
With respect to brackets, ¬(X ∨ Y ∨ Z) is similar to -(a + b + c) on integers: you first add a, b and c and the find the opposite of that sum. Similarly, in ¬(X ∨ Y ∨ Z), you take the disjunction of X, Y and Z (strictly speaking, you first find X ∨ Y and then take a disjunction with Z), and then apply the negation.

Due to De Morgan's laws, the first two formulas are equivalent, as are the last two. You are right that the first (and hence the second) formula is correct. To show that the last two formulas are incorrect, find some truth values of X, Y and Z such that ¬X ∨ ¬Y ∨ ¬Z is true, but GREEN is not executed.
• Jul 7th 2011, 03:02 AM
iwan1981
Re: if and else statements rogether with propositionlogic
So this is what you are saying:

¬X∧¬Y ∧¬Z = GREEN EXECUTED
¬(X∨Y ∨Z) = GREEN EXECUTED
¬X∨¬Y ∨¬Z = GREEN NOT EXECUTED
¬(X∧Y ∧Z) = GREEN NOT EXECUTED

And that the first 2 are actually the same due to "Demorgans Law"
So ¬X∧¬Y ∧¬Z becomes ¬X∧¬Y ∧¬Z when you remove the brackets of ¬(X∨Y ∨Z) ...
Is that correct?
• Jul 7th 2011, 03:16 AM
emakarov
Re: if and else statements rogether with propositionlogic
Quote:

Originally Posted by iwan1981
So this is what you are saying:

¬X∧¬Y ∧¬Z = GREEN EXECUTED
¬(X∨Y ∨Z) = GREEN EXECUTED
¬X∨¬Y ∨¬Z = GREEN NOT EXECUTED
¬(X∧Y ∧Z) = GREEN NOT EXECUTED

Strictly speaking, I did not say that if ¬X ∨ ¬Y ∨ ¬Z is true, then GREEN is not executed. Both ¬X ∧ ¬Y ∧ ¬Z and ¬X ∨ ¬Y ∨ ¬Z can be true. In fact, if the first of these formulas is true, i.e., X = Y = Z = F, then so it the second. However, it can be that the second formula is true, but GREEN is not executed.

Quote:

And that the first 2 are actually the same due to "Demorgans Law"
Yes.

Quote:

So ¬X∧¬Y ∧¬Z becomes ¬X∧¬Y ∧¬Z when you remove the brackets of ¬(X∨Y ∨Z) ...
¬(X∨Y ∨Z) becomes ¬X∧¬Y ∧¬Z, yes.
• Jul 7th 2011, 03:25 AM
iwan1981
Re: if and else statements rogether with propositionlogic
Quote:

it can be that the second formula is true, but GREEN is not executed.
This part confuses me a little bit ...
Because with that said ... this means that ¬(X∨Y ∨Z) can be either GREEN EXECUTED or GREEN NOT EXECUTED ...

And that this is wrong then:

¬X∧¬Y ∧¬Z = GREEN EXECUTED
¬(X∨Y ∨Z) = GREEN EXECUTED
¬X∨¬Y ∨¬Z = GREEN NOT EXECUTED
¬(X∧Y ∧Z) = GREEN NOT EXECUTED
• Jul 7th 2011, 03:47 AM
emakarov
Re: if and else statements rogether with propositionlogic
Quote:

Originally Posted by emakarov
it can be that the second formula is true, but GREEN is not executed.

Quote:

Originally Posted by iwan1981
This part confuses me a little bit ...
Because with that said ... this means that ¬(X∨Y ∨Z) can be either GREEN EXECUTED or GREEN NOT EXECUTED ...

In saying "the second formula," I referred to the immediate context in my post, not to your original post. I meant that ¬X ∨ ¬Y ∨ ¬Z can be true, but GREEN is not executed.

Quote:

And that this is wrong then:

¬X∧¬Y ∧¬Z = GREEN EXECUTED
¬(X∨Y ∨Z) = GREEN EXECUTED
¬X∨¬Y ∨¬Z = GREEN NOT EXECUTED
¬(X∧Y ∧Z) = GREEN NOT EXECUTED
Yes, the last two lines are wrong in that neither the left- nor the right-hand side imply the other.

There are only 8 sets of truth values for X, Y and Z. You can go through all of them and see which formulas are true and which code is executed.
• Jul 7th 2011, 04:07 AM
iwan1981
Re: if and else statements rogether with propositionlogic
So your actually saying that in all options GREEN can be executed ...
• Jul 7th 2011, 04:21 AM
emakarov
Re: if and else statements rogether with propositionlogic
Quote:

Originally Posted by iwan1981
So your actually saying that in all options GREEN can be executed ...

Yes. When the first two formulas from the OP are true, GREEN has to be executed; when the last two formulas are true, GREEN may or may not be executed.
• Jul 7th 2011, 04:24 AM
iwan1981
Re: if and else statements rogether with propositionlogic
Quote:

Originally Posted by emakarov
Yes. When the first two formulas from the OP are true, GREEN has to be executed; when the last two formulas are true, GREEN may or may not be executed.

What do you mean with forst 2 formula's from OP ...
I think I have lost you :-)

Sorry :-(
• Jul 7th 2011, 04:27 AM
emakarov
Re: if and else statements rogether with propositionlogic
"OP" stands for the "original post." It is the first post in the thread. The first two formulas in that post are ¬X ∧ ¬Y ∧ ¬Z and ¬(X ∨ Y ∨ Z) (they are equivalent).
• Jul 7th 2011, 04:31 AM
iwan1981
Re: if and else statements rogether with propositionlogic
I think I have the concept now ...

¬X∧¬Y ∧¬Z = GREEN EXECUTED
¬(X∨Y ∨Z) = GREEN EXECUTED
¬X∨¬Y ∨¬Z = GREEN NOT EXECUTED / OR MAY BE EXECUTED
¬(X∧Y ∧Z) = GREEN NOT EXECUTED / OR MAY BE EXECUTED

So the last 2 formula's is correct either way ... if we only have 2 answer options there (Executed of Not Executed)

Thanks!