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
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
Post your code, comment on any confusion or compiler error(s)/warning(s). Visit the FAQ.
Think of the logical operators just as you would with the same words in the english language.
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
Thanks,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
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
>.< 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 ?
Hope I didn't get the meaning wrong.Quote:
0 && 0 = 0 //0 and 0 equal FALSE
Edit : Hey I think I got what it means, thanks man for your simple table. :D *Jumping with joy*
Thanks,
hackerkts
In C++, 0 evaluates to false, and anything non-zero, like 1, evaluates to true.
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?
Nope, thank you sir !
That's a nice example too :D