# [newb] How is "!(1 && !(0 || 1))" true?

This is a discussion on [newb] How is "!(1 && !(0 || 1))" true? within the C++ Programming forums, part of the General Programming Boards category; Hello there, I've just signed up to this forum after getting confused with the 2nd quiz in the tutorial. First ...

1. ## [newb] How is "!(1 && !(0 || 1))" true?

Hello there, I've just signed up to this forum after getting confused with the 2nd quiz in the tutorial. First up I just want to say how pleased I am to have found this website, it is really great!

Years ago I learned how to program quite well in BASIC, i made quite a few games as personal projects. Yesterday I began learning C++ thanks to this site, after downloading Dev-C++

So far everything is going fine, last night I spent ages on the first tutorial, making notes as I went along, trying the examples, altering them, and basically spending as much time on it as I could. Today I woke up and tried the 2nd, with if statements. This was all a lot more familiar to me from BASIC (apart from using && instead of and etc).
The only thing that has confused me is a part of the quiz.

3. Evaluate !(1 && !(0 || 1)).
A. True
B. False
C. Unevaluatable

Apparently the answer is A, True, but I would appreciate it if someone please explained to me how this is possible?

When I see that, I read it as:

NOT(1 AND NOT(0 or 1))

If something isnt (0 or 1, plus 1), then how can it return true?

Thanks for any help, I'm just a bit baffled at this, everything else is going swimmingly so far. Cheers!

2. Go step by step:

0 OR 1 = 1
NOT(0 OR 1) = NOT(1) = 0
1 AND NOT(0 or 1) = 1 AND 0 = 0
NOT(1 AND NOT(0 or 1)) = NOT(0) = 1

1 is True

3. ## thanks!

Ok thankyou, I think I get it now.
(0 OR 1) = 1?
I thought it was 0 OR 1, not just 1.

Surely that is like saying blue or green is green, even though it could be blue.

I think I do understand you but the logic seems a little... illogical.

4. I see! I was just looking at your text saying "1 AND 0 = 0" and though erm..

But now I see, "TRUE AND FALSE = FALSE"

Which means "TRUE OR FALSE = TRUE"

Thanks for helping me clear it up, I just want to make sure I fully understand everything I come across before I let myself move on, I'm really determined to get good at C++.

Whoopee! Bye for now.

5. Surely that is like saying blue or green is green, even though it could be blue.
That analogy is not correct. Here we are dealing with boolean values.

6. Originally Posted by eddwills
Surely that is like saying blue or green is green, even though it could be blue.
It's more like saying "sky is green - or - grass is green" which is true because grass really is green. The colour of the sky doesn't make a difference.

7. Yup, sorry for that, I understand now though. Thanks again for the help.

8. ## Another question now.

Hello again!
I figured making a new thread would've been a bit of a waste of space, so I have another question here.
It is from the quiz on "Lesson 5: Switch Case".
4. What is the result of the following code?
Code:
```x=0;

switch(x)

{

case 1: cout<<"One";

case 0: cout<<"Zero";

case 2: cout<<"Hello World";

}```
A. One
B. Zero
C. Hello World
D. ZeroHello World
Can someone tell me how when x=0 x manages to fulfil two cases?
is it because there is no break? I'm guessing it's purposefully bad programming, and should be:
Code:
```int x=0;

switch(x) {

case 0:
cout<<"Zero";
break;
case 1:
cout<<"One";
break;
case 2:
cout<<"Hello World";
break;
}```
Is this all right? It's not so much of a question, more something I'd like someone with a bit more knowledge to clear up. Obviously having this example is useless alone, as x will only ever equal 0, and also there is no default, but asides from that is everything present ad correct? Thanks a lot

9. >>which is true because grass really is green

not where I live -- its been brown now for a few months.

10. You are right, it is because there is no 'break.' The execution "falls through" the rest of the cases. Because most of the time this is not desired you see switch statements with break after each case more often than not.

There are some uses for it though. For example:
PHP Code:
``` int daysInMonth; switch (month) {     case 1: case 3: case 5: case 7: case 8: case 10: case 12:         daysInMonth = 31;         break;     case 4: case 6: case 9: case 11:         daysInMonth = 30;         break;     case 2:         daysInMonth = isLeapYear ? 29 : 28;         break;     default: /* unknown month?? */ }  ```

11. Originally Posted by Ancient Dragon
>>which is true because grass really is green

not where I live -- its been brown now for a few months.
At least you can see it... here I can't check because it's below some vile white stuff

12. Hehe, thanks joni. So while the example in the tutorial is bad programming, excluding breaks may be used on purpose for good. C++ is so much better than BASIC! Even things like having different variables to ensure a digit is an integer or whatnot amazed me, I can't wait to get onto more advanced coding. Thanks again for your help, You'll no doubt see me again at some point.

Bye for now!

Popular pages Recent additions