|
| Aakanaar (166) | ||||||||||||||||||||||||||
I have yet again seen a piece of code similar to the following very common mistake
Before I get into why that first if will always return true, let's look at a few details about how conditionals work. What is a conditional? A conditional is a statement that evaluates to true or false. All of your control structures rely on conditionals. This includes..
And since conditionals evaluate to true or false then the above can also be writen as such..
Naturally, if (true) would defeat the purpose of the if statement, but while (true) is very commonly used. So how do we define True or False? A lot of people would tell you that false is zero, and true is any number larger than zero. This is not entirely accurate. To be more percise, false is zero and true is any non-zero. This includes negative numbers. (I have tested to confirm this with DevC++) False can also be null which is distinctly different from zero. There are many functions that return null, so this can also be used as a conditional. So what qualifies as a conditional? Anything that evaluates to true or false. All of your comparisons, such as greater than '>', less than or equal to '<=', and is equal to '==' evaluate to true or false.
All of your assignments also evaluate to true or false. This is because assignments also return a value. a = 5; stores the number 5 into the variable a. But it also returns the value 5. This is why b = a = 5; works. It first stores 5 into a, then returns 5 which is stored into b. The b = 5; also returns 5, but since we don't store it into anything else, it gets lost into time and space, never to be seen again. This is ok, since someday someone will invent a hyperspace engine that gets its power from all of these lost values floating around.
Then finally, there are many functions that you can call that return values. Any function that does not return a void (or by definition of void does not return anything) can be used as or in conditionals. Logical Operators There are two logical operators that can be used to concatenate conditionals. These are the And '&&', and the Or '||' operators. both of these take two conditionals, one on either side, and evaluate each conditional seperately.
And this brings us back to the code at the beginning, and why it will always return true. That code reads...
Special Note You can also use assignments and function calls with the logical operators. However, special care should be given here, as doing so may have unexpected results. This is due to the way the program handles these logical operators. For example, Or returns true if either conditional is true. It checks the first conditional, and if it returns false, it will check the second conditional. If, however, the first conditional returns true, it does not need to check the second conditional. It already has enough information to return true. Let's take the following code for example..
One would think from looking at this code that both i and j would reach 10. However, once i reaches 5, it returns true and thus the second conditional no longer needs to be tested. The result is the following output.
And, however, requires both conditionals to be true. So it checks the first conditional, and if it returns true, it will check the second conditional. If, however, the first conditional returns false, there is no reason to check the second conditional. It already has enough information to return false. Thus the following code..
.. produces the following output..
The Not Operator And there is of course, one more logical operator to consider. the Not '!' operator which when put in front of the conditional, simply reverses it. It turns true into false and false into true.
Side Note: XOR I originally had Xor as a logical operator, but it is not. It is a bitwise operator. It is possible, however, to emulate the Xor functionality in a logical sence. Xor is simply 'Or And Not And'. However, using assignments in part or all of this is much more difficult to predict the possible outcomes.
..produces this output..
| ||||||||||||||||||||||||||
Last edited on | ||||||||||||||||||||||||||
| Aakanaar (166) | |
| (thanks to whoever moved it back to articles for me.) Anyone see anything that needs corrected or added to the above? Anything that may make any of the above more clear to the readers? | |
Last edited on | |
| QWERTYman (367) | |
Perhaps discussion on concatenating conditions; likeif(h == 5 || (i == 5 && j == 5) ), or using the conditional operator; like h == 5? 7 : 6;but overall, purty good. | |
| Aakanaar (166) | |
the conditional operator h = 5 ? 7 : 6;was sort of outside of my scope here, but i'll look into it. As for the concatenating conditionals such as if (h == 5 || (i == 5 && j == 5) ) is something i should add in, i was thinking of going a bit further with my examples showing how if (i >= 5) evaluates to if (false) and if (true) Then showing how if (i >= 5 || if j >= 5) evaluates to if (false || true) wich evaluates to if (true) If i did that, then going the next step and having if (h == 5 || (i >= 5 && j >= 5) ) evaluates to if (h == 5 || (true && true) ) evaluates to if (false || true) evaluates to if (true) but i was woried i might confuse some people with that. | |
Last edited on | |
| Aakanaar (166) | |
| Ok, updated. Looks a bit better. Gives examples. Had some trouble with notepad putting in random line breaks, but turning off word wrap (after having written it) fixed that problem. Changing title to remove the [Draft] part. anyone else have any suggestions? sorry, made a few corrections to the main article and added something else, to the end. | |
Last edited on | |
| Grey Wolf (1617) | |||
| There is a small... The XOR [^] operator is a bitwise operator, not a logical operators. Using it in a Logical context could produce an unexpected result. For an example, if you had a couple of functions that returned zero for failure and non-zero for success and you XORed the returned value, both can return success (true) but with different values, but the XOR would return true instead of false, because it is doing a bitwise comparison.
| |||
Last edited on | |||
| Aakanaar (166) | |
| ok.. odd. for some reason I could have sworn when I looked that up originally that it was grouped with the logical operators. It's on the same page, but as I just looked, further down on the page, with the bitwise. I was seeing things. heh. So there is no Logical XOR? only the bitwise XOR? hrm.. there is a way to do it, but not in the context of what I have up above. XOR is simply 'Or and Not And' wich is: ((a || b) && ! (a && b)) but if you tried to use ++a and ++b in all or even part of that, it could get very confusing what the results will be. Ok.. corrected. Thanks Greywolf | |
Last edited on | |
| sphenlee (1) | |
| There actually is a logical xor operator, however most people know it as 'not equal'. If you compare the 4 combinations of true and false for logical xor and != you will see they give the same results. You must be careful though, since != will not convert the values to booleans first. So if(conditionA != conditionB) will work, but if(valueA != valueB) may not. You can get around this by typecasting to bool before comparing. | |
| Duoas (3510) | |
| Logical operations != Bitwise operations Be careful on what assumptions you make. 0100 --> true true xor 0010 --> true != true -------- -------- 0110 --> true false | |
| firedraco (2624) | |||
A logical xor is basically this:
Although there actually ISN'T one, the only logical operators are: AND & OR v NOT ~ IF_THEN <horseshose symbol> IF AND ONLY IF <triple bar symbol> | |||
Last edited on | |||
| Duoas (3510) | |
| Er, actually there are a couple more. ∧ conjunction http://en.wikipedia.org/wiki/Logical_conjunction ⋁ disjunction http://en.wikipedia.org/wiki/Logical_disjunction ¬ negation http://en.wikipedia.org/wiki/Logical_negation → implication http://en.wikipedia.org/wiki/Logical_implication ↔ bi-implication http://en.wikipedia.org/wiki/Bi-implication ∀ universal quantifier http://en.wikipedia.org/wiki/Universal_quantifier ∃ existential quantifier http://en.wikipedia.org/wiki/Existential_quantifier Alas, there are a lot of variations on the actual symbols used for logical operators. The ones I listed are the classical set (used in predicate calculus, aka first-order logic). The Wikipedia links lists others. The one to watch out for is ≡, which is often seen in congruence relations, for example 11 ≡ 5 (mod 2) hence 11 ≡ 2 (mod 5). This is a branch of mathematics unrelated to predicate calculus. Others exist either to accomodate computers or to distinguish problem domain and/or application. Programming languages lend themselves to predicate calculus. A predicate is essentially a boolean function. Mathematically, I can give the example: Bx means 'x' is a bird Tt means 't' is a tree Hxt means that the bird 'x' is lives in the tree 't' I can construct logical sentences thus: Bx → ¬Tx if 'x' is a bird then 'x' cannot be a tree (a true statement) ¬Tx → Bx if 'x' is not a tree then 'x' must be a bird (a false statement) hence Bx ↔ ¬Tx 'x' must be either a bird or a tree (again false) The reason goes straight to the truth tables learnt in high-school. Check out http://en.wikipedia.org/wiki/Truth_table for more. Also play with google some for more fun. :-) Enjoy! [edit] Heh, forgot to use H. ∀x(Bx → ∃t(Hxt)) Remember N(x) is a predicate. See if you can decipher this sentence and determine whether it is true or false. :-) | |
Last edited on | |
This topic is archived - New replies not allowed.
