I'm doing the following:
This doesn't evalulate as true even if x or y = someChar. Is the syntax wrong for a char type?Code:if (('x' || 'y') == someChar) { //statements; }
I'm doing the following:
This doesn't evalulate as true even if x or y = someChar. Is the syntax wrong for a char type?Code:if (('x' || 'y') == someChar) { //statements; }
The statement:
probably evaluates to true (1), so you then do this:Code:('y'||'x')
Try:Code:if (1==someChar)
Code:if ((someChar=='y') || (someChar=='x'))
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
That works too. I tried doing the opposite but it doesn't work:
This is always true:
This is always false:Code:('x' || 'y' == someChar)
Why is this?Code:(('x' || 'y') == someChar)
'x' typically has a non-zero value, so ('x' || anything) will always be true (and, thanks to the wonders of shortcircuiting evaluations) the anything will never be evaluated.
The additional brackets in the second example mean that 'x' || 'y' will be evaluated first, and the result of that comparison will be compared with someChar. At least one of 'x' or 'y' will have a non zero value, so 'x' || 'y' typically evaluate as 1. Which means that (('x' || 'y') == someChar) will be evaluated to false (zero) unless someChar is equal to 1.
Code:if(('x' == someChar) || ('y' == someChar)) { //do something } else { //do something else }
Two notes regarding these comments: 1) 'x' is a non-zero value (not just typically). 2) it is not necessarily true that 'anything' would never be evaluated because the order of evaluation is implementation dependant. (Note that I'm not ragging on grumpy; I just want to avoid people being mislead by false assumptions.)Originally Posted by grumpy
iSlak, you have to take into consideration what types of values are returned by different operations. When you use the logical OR operator (||), the result is always a boolean true or false (1 or 0, respectively). So, regardless of the context of the statement, any time you say (whatever || whocares), it will yield a true or a false. Thusly, if you compare the result of that statement with the equality operator (==), like this: (whatever || whocares) == somejunk, you are actually comparing if somejunk is equal to true or false, depending on the result. With that said, your OR statement is always returning true because what it says is "if 'x' is non-zero (and it is not zero) OR if 'y' is non-zero (and it too is not zero) return true otherwise return false." The only way your if statement would resolve to true is if someChar contains the numeric value 1.
Whenever you would like to determine if a variable is one of several values, you must make the comparisons one-by-one. In other words, your statement should say "if someChar is equal to 'x' OR if someChar is equal to 'y'" because in C/C++ you can not say "if someChar is equal to one of: 'x' or 'y'."
If C++ follows C, then it may be you that is doing so.Originally Posted by LuckYUnlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares unequal to 0, the second operand is not evaluated.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
This is making some sense now. What do you all mean by a "non-zero" value?
Much like it says, a value that is not zero. Is 'x' a zero -- that is, a character used for null termination? If it isn't, which it isn't, it's non-zero.Originally Posted by iSlak
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
any value that is not 0 (zero)
a few non-zero would be
Code:123456789-=~`!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?qwertyuiop[]\asdfghjkl;'zxcvbnm,./
>If C++ follows C, then it may be you that is doing so.
C++ indeed follows C in this regard.
Hmm. I'm curious now myself. My information was provided a few years back by a C++ instructor of several years. I have not had the pleasure of being privy to a confirmation or contradiction since (at least not specifically regarding C++).
Would you mind quoting your source or a reference that might substantiate this claim?Originally Posted by swoopy
hmm... doesnt the C++ standard say that:2) it is not necessarily true that 'anything' would never be evaluated because the order of evaluation is implementation dependant. (Note that I'm not ragging on grumpy; I just want to avoid people being mislead by false assumptions.)
"The || operator groups left-to-right. The operands are both implicitly converted to bool (clause 4). It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true."
(ISO/IEC 14882:2003 section 5.15)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
speaking of the standard, id like a copy fo the standard
in pdf format, i wish it was free, it should be free, grr.