# Thread: The 1 and 0.. True/false - CONFUSED!

1. ## The 1 and 0.. True/false - CONFUSED!

Hi all,

So I've been learning for only a couple days, so please, be gentle.

I'm up to chap 2 (tutorial on this site) and the true/false (0-1) and the NOT, AND, OR stuff.

I'm going ok until I hit this spot and am quite confused as the tutorial didn't go into any detail about it (the code below). In english, can u please break down this so I can understand it.
Code:
`!(1 || 0) ANSWER 0.`
So the above, I'm guessing here, but.. the ! is a NOT, so I am assuming that it is saying the 2 inside numbers are not equal? Or False? So would:
Code:
`!(1 || 1) be 1?`
The question I have is what is the exact meaning of this code? Why wouldn't we just put 1 > 0.

I'm sure is is a really stupid question, but I can't understand why this code is the way it is. In the tutorial (on this site by the way) it doesn't go into any specifics of why they are like this.

And, just for kicks, can someone break down what this is?
Code:
`!( ( 1 || 0 ) && 0 ) Answer 1`
I mean what the ????. The tutorial just jumped right into this. It didn't say where this would come in handy or in what context I would ever use this? Is it just an example or?

Any help is muchly appreciated.

Cheers,

2. When an integer is converted to bool, zero is converted to false and non-zero is converted to true. When a bool value is converted to int, false is converted to 0 and true is converted to 1.

Now, the operands of the built-in operator|| are of type bool. Therefore, you can interpret (1 || 0) as (true || false), which evaluates to true. !true == false, so the expression !(1 || 0) evaluates to false, hence the 0.

3. Code:
```TRUE  and TRUE  = TRUE
TRUE  and FALSE = FALSE
FALSE and TRUE  = FALSE
FALSE and FALSE = FALSE

---

TRUE  or TRUE  = TRUE
TRUE  or FALSE = TRUE
FALSE or TRUE  = TRUE
FALSE or FALSE = FALSE

---

not TRUE  = FALSE
not FALSE = TRUE```
Therefore:

Code:
```!(1 || 0) ANSWER 0.
NOT (1 OR 0) = 0
NOT (1) = 0

!(1 || 1)
NOT (1 OR 1) = NOT (1) = 0

!( ( 1 || 0 ) && 0 ) Answer 1
NOT ( (1 OR 0) AND 0) = 1
NOT ( (1) AND 0) = 1
NOT (0) = 1```

4. Thanks for that guys. I see what you are saying Matticus and thanks so much for the easy breakdown and I now see what you mean. (thanks to you to laser as I learned something from what you said too ).

Can u guys tell me though, do u have an example of where this would run in a code (simple please). My mind works better when I see something in action rather than just meaningless code. Sorry if not, as I said, just learning this stuff.

5. Originally Posted by Crazycanook
Can u guys tell me though, do u have an example of where this would run in a code (simple please).
Sure, e.g.,
Code:
```#include <iostream>

int main()
{
using namespace std;
cout << !( ( 1 || 0 ) && 0 ) << endl;
}```

6. Thanks .

So can u tell me why you use that exactly. Not the program, but the numbers and brackets and all of that.

For what reason would u need to type that just to have it spit back a False. Isn't there an easier way to have it spit out a False/True

I think once I can figure out why I can move on.

7. This is just an academic exercise. In practice most of those integer/boolean literals would be variables or the return values of functions, and the expression could be the condition of an if statement or a loop, thus determining the flow of control.

8. Thanks for all of your help!

9. !(1 || 0) ANSWER 0.

Logical operators are usually used to apply on the result of the two or more conditional statements that returns either true or false.
like if((a>b) ||(a>c)).

Any non zero value for these operators will be considered as "true" (includes negative numbers also) and zero value will be considered as "false".

value for the true is "1"
value for the false is "0"

1 || 1 = 1
1 || 0 = 1
0 || 1 = 1
0 || 0 = 1

!(1) = 0
!(0) = 1

first (1||0) will be computed the output will be 1 based on the above table

!(1) will be computed the result of which will be 0

Remeber any non zero value will be "true" and zero value will be false for the logical operators.

Hope it has cleared your doubt.

10. Think of a true statement and a false statement.

Eg.
1. Cats are mammals (true)
2. The moon is made out of cheese (false)

Code:
`!(1 || 0)`
Means -
NOT (cats are mammals OR the moon is made out of cheese)

"cats are mammals OR the moon is made out of cheese" is true, because cats are mammals.
NOT of the statement makes it false (by definition).

Code:
`!( ( 1 || 0 ) && 0 )`
That's the same as

NOT ( (cats are mammals OR the moon is made out of cheese) AND the moon is made out of cheese)
(cats are mammals OR the moon is made out of cheese) is true, but that AND the moon is made out of cheese isn't. Because the moon isn't made out of cheese.
NOT of that gives you true.

11. This is probably a little too advanced for now (or maybe not), but you'll notice that in the second example, (1 || 0) && 0, it doesn't matter what the first part is, the answer has to be false.

"xxx AND the moon is made out of cheese" is false no matter what xxx is.

This leads to something called short circuit evaluation.

Similarly

"xxx OR cats are mammals" is true no matter what xxx is.

12. Originally Posted by cyberfish
This is probably a little too advanced for now (or maybe not), but you'll notice that in the second example, (1 || 0) && 0, it doesn't matter what the first part is, the answer has to be false.

"xxx AND the moon is made out of cheese" is false no matter what xxx is.

This leads to something called short circuit evaluation.
No, it doesn't. Conditions are tested in sequence, from left to right.
So "The moon is made out of cheese AND xxx" will never look at xxx, because the first part is always false. But if the xxx is on the left side, it will be looked at, and it's "the moon is made out of cheese" that won't be looked at if xxx turns out to be false.

13. Ah yes I got the order wrong for C/C++. Thanks.

I was thinking more in terms of logic minimization.