Hi, I'm trying to go through the C++ tutorials, but I keep getting stumped when i hit the Boolean stuff. Could somebody please explain it in an easier way? Thanks
This is a discussion on Boolean operators within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to go through the C++ tutorials, but I keep getting stumped when i hit the Boolean stuff. ...
Hi, I'm trying to go through the C++ tutorials, but I keep getting stumped when i hit the Boolean stuff. Could somebody please explain it in an easier way? Thanks
If you narrow down your question, I'm sure your problems will be addressed more promptly and you will understand much better than if some kind person came along and wrote up a big page about boolean logic. Perhaps, you could also use the resources the internet provides to help you narrow down your question:
http://computer.howstuffworks.com/boolean.htm
http://www.intap.net/~drw/cpp/cpp03_04.htm
http://www.cprogramming.com/tutorial/lesson2.html
etc. etc. etc. google. google.
Ok, I'm working on the tutorial 2, and I sort of understand the NOT operator, but the AND and OR are just beyond me. It gives this example:
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)
For A, i think this means NOT 1. 0 is not 1 so this would be true but the answer is 0 meaning false.
For starters, what am I not understanding in A?
Holy deja vu, Batman! This was covered just a few posts ago:
so confused - learning something
I guess you have !(been reading the posts);
-JM
ok, i read that and it helps, but one question still:
How does ( 1 || 0 ) equal to 1?
It means 1 or 0. Does the OR operator only look for either of the values to be one and then it is true? Can the Or operator ever be looking for a zero? How about a seven?
Edit: I think I understand now. I had been thinking of 0 and 1 as numbers in the questions. Instead I should be thinking of them as true and false. This makes a lot more sense and gets me the correct answers for the examples. Now I can finally get on to tutorial 3 and continue my path to world domination.
Last edited by Trogdor27; 09-11-2005 at 01:15 PM.
Yes, OR returns true (1) if either one are true (ie. if either values is true it will do the next section of code). No, OR wont return true if both are false (0), I think you might be confusing what exactly OR does. You use OR in combination to things like ==, !=, !, and those are what return 1 or 0: ie. if (someVariable == 0 || somevariable == 100) - lets say someVariable is equal to 100, so that statement becomes: (0 || 1), the right side is true, and therefor the if statement procedes. Of course there are some cases where what you said (if its 7) will occur, but its not extremely common: ie. if ( someFunction() || anotherFunction() ) - what this does is takes the value returned from each function, someFunction could return 7 and itd be considered true - (7 || 0) lets say, which is 1. True in C++ is clasified as non-zero, so 1, 7, 14, 1000 are all 'true' (anything not 0).Originally Posted by Trogdor27
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
Excellent, thank you for clarifying. Its all coming together now.
You might be thinking of these as logical statements. Also think of the logical operators as mathematical functions. The ! operator is simply defined by f(x) = 0 if x != 0, 1 if x = 0.
The || operator is simply defined by f(x,y) = 0 if (x,y) = (0,0), 1 otherwise.
The && operator is simply defined by f(x,y) = 0 if x = 0, 0 if y = 0, 1 otherwise.
(Assume that true and false are stand-ins for 1 and 0.)
The || and && operators will stop after evaluating their left hand side if they can already deduce the answer, so sometimes, the right side does not get evaluated. It's good to know this.
Trogdor27, I noticed you use your text gun as a sig, and yet it's not. If you made it a sig you wouldn't have to add it to each post.
Hmm?Originally Posted by Cool-August
Quoting him doesnt seem to catch his gun sig.. doesnt seem like hes adding it manually. (nor is it included in the quotes below this text box)
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101