0

Understanding Boolean logic

This a small question i've been a little dismissive of for a long time until now: why, in the example statement below, is the &&(and) acting as if it were an or statement(||)? while (choice != "Attack" && choice != "Defend" && choice != "Heal") { (do smth.) } an example theory I've come up with is that: - usually, and statements check for that two statements are BOTH true. in this case there is only one statement, meaning that only one check needs to return false for the loop to not execute. - in the opposite case, no cases are false, meaning that the whole loop would be true and executed. can someone clarify this for me? it's slightly confusing, although slightly trivial.

3rd Jan 2018, 6:44 PM
X-1
X-1 - avatar
4 Answers
+ 2
You just discovered the de-Morgan-rule, very perceptive! Suppose you have boolean variables a and b (a can take the values true or false, the same holds for b). Then !(a && b) will always be the same as !a || !b. The ! is the negation in this case. For example: If a is True and b is False, a && b will be False, hence !(a && b) is True. Also, !True || !False is the same as False || True, which is True as well. You can check for yourself, that this identity also holds for the other 3 possible value pairs a,b can take (True,True; False,False; False,True). If you have done that, figure out how this is connected to your question and you might answer it yourself. If you get stuck, just ask me again and I'll explain more.
3rd Jan 2018, 11:38 PM
Tob
Tob - avatar
+ 1
In what way does it behave like an or statement? Do you have an example code? Boolean operators, just like arithmetic operators, are binary in that they take two operands. In order to chain more statements you evaluate according the rules (e.g. order of operations) so the statement A && B && C will just evaluate A && B (lets suppose that this is true) then we evaluate true && C. In this case I'd say there were 3 statements, not 1 (the presence of operators tells us there must be more than 1!) You could convert this to OR statements if you had !(choice == "Attack || choice == "Defend" || choice == "Heal") so if any one condition holds, we don't 'do smth', we only do when none of these conditions hold. Question is worth asking, but might need a little more clarification :)
3rd Jan 2018, 7:29 PM
Dan Walker
Dan Walker - avatar
0
with && And A must equal B (both A and B must be true to give true) with II Or A Or B must be true to give true (1 True enuff to give True)
3rd Jan 2018, 7:52 PM
Elie Douaihy
Elie Douaihy - avatar
- 2
First, the values in your example are not of boolean type, as there's no true/false. A Boolean type variable has two possibilities, and if one is verified, the other one isn't. So, you couldn't make a statement with three conditions. Your code will run only if "choice" is a word equal to NONE of the three conditions. So that e.g., you have choice = crocodile, your code will run, but if choice = Defend, it won't. Just don't make things harder :)
3rd Jan 2018, 6:59 PM
Luca Marseglia
Luca Marseglia - avatar