Hey all, greeting from Singapore 16M. I'm just started reading the tutorials today and I'm really interested to C++.

I don't understand Boolean operators, 'NOT', 'AND' and 'OR'.

Can someone help me out this part ?

Thanks,

hackerkts

Printable View

- 02-11-2006hackerkts[Help] Tutorial : If Statements
Hey all, greeting from Singapore 16M. I'm just started reading the tutorials today and I'm really interested to C++.

I don't understand Boolean operators, 'NOT', 'AND' and 'OR'.

Can someone help me out this part ?

Thanks,

hackerkts - 02-11-2006Dave_Sinkula
Post your code, comment on any confusion or compiler error(s)/warning(s). Visit the FAQ.

- 02-11-2006OnionKnight
Think of the logical operators just as you would with the same words in the english language.

- 02-11-2006hackerkts
Thanks for the reply, I don't the whole part for Boolean, how do I know the result is 0 or not ?

Just like the example given in the tutorial

Code:`A. !( 1 || 0 ) ANSWER: 0`

B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)

C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

hackerkts - 02-11-2006OnionKnight
Here's a simple truth table:

0 && 0 = 0

0 && 1 = 0

1 && 0 = 0

1 && 1 = 1

0 || 0 = 0

0 || 1 = 1

1 || 0 = 1

1 || 1 = 1

!0 = 1

!1 = 0 - 02-11-2006hackerkts
>.< Do you mind giving me more details ? Sorry, this my 1st time learning on C++ need more times to understand basic stuff. Eh I mean the example you given can you indicate what does those means ?

Quote:

0 && 0 = 0 //0 and 0 equal FALSE

: Hey I think I got what it means, thanks man for your simple table. :D *Jumping with joy**Edit*

Thanks,

hackerkts - 02-12-20067stud
In C++, 0 evaluates to false, and anything non-zero, like 1, evaluates to true.

- 02-12-2006SlyMaelstrom
The AND operator says that if any part (the left value or right value) evaluates to false, then the whole statement must be false. The OR operator says that if any part (the left value or right value) evaluates to true, then the whole statement must be true.

Let's put it this way:

Say your mom gives you two chores to do... "Rake the leaves AND mow the lawn." If you only rake the leaves, you still haven't done your chores. If you only mow the lawn, you still haven't done your chores. You must do both of them in order to complete your chores. Now lets say she was giving you an option... "Clean the counters OR mop the floor." If you Clean the counters, you've done your chore. If you mop the floor you've done your chore. If you do both, you've still done your chore and then some. If you do neither, obviously you haven't done your chore.

The NOT operator simply just flips the truth of the statement around. NOT TRUE equals FALSE and NOT FALSE equals TRUE and NOT (TRUE && TRUE || FALSE && TRUE) equals FALSE because the interior evaluates TRUE.

The order of operations as far as these go are in the order: NOT, AND, OR. If you take that last statement I wrote, NOT (TRUE && TRUE || FALSE && TRUE), and remove the parenthesis, NOT TRUE && TRUE || FALSE && TRUE, it should properly be read as this:

(NOT TRUE) && TRUE || FALSE && TRUE

or

FALSE && TRUE || FALSE && TRUE

or

FALSE || FALSE

equals

FALSE

Any questions? - 02-12-2006hackerkts
Nope, thank you sir !

That's a nice example too :D