# Comparison Operator Comparing Two Values ('y' || 'x' == blah)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-20-2005
iSlak
Comparison Operator Comparing Two Values ('y' || 'x' == blah)
I'm doing the following:
Code:

```if (('x' || 'y') == someChar) {         //statements; }```
This doesn't evalulate as true even if x or y = someChar. Is the syntax wrong for a char type?
• 08-20-2005
JaWiB
The statement:
Code:

`('y'||'x')`
probably evaluates to true (1), so you then do this:
Code:

`if (1==someChar)`
Try:
Code:

`if ((someChar=='y') || (someChar=='x'))`
• 08-20-2005
iSlak
That works too. I tried doing the opposite but it doesn't work:

This is always true:
Code:

`('x' || 'y' == someChar)`
This is always false:
Code:

`(('x' || 'y') == someChar)`
Why is this?
• 08-20-2005
grumpy
'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.
• 08-20-2005
ILoveVectors
Code:

```if(('x' == someChar) || ('y' == someChar)) {     //do something } else {     //do something else }```
• 08-20-2005
LuckY
Quote:

Originally Posted by grumpy
'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.

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.)

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'."
• 08-21-2005
Dave_Sinkula
Quote:

Originally Posted by LuckY
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.)

If C++ follows C, then it may be you that is doing so.
Quote:

Unlike 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.
• 08-21-2005
iSlak
This is making some sense now. What do you all mean by a "non-zero" value?
• 08-21-2005
Dave_Sinkula
Quote:

Originally Posted by iSlak
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.
• 08-21-2005
ILoveVectors
any value that is not 0 (zero)
a few non-zero would be

Code:

`123456789-=~`!@#\$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?qwertyuiop[]\asdfghjkl;'zxcvbnm,./`
• 08-21-2005
swoopy
>If C++ follows C, then it may be you that is doing so.
C++ indeed follows C in this regard.
• 08-21-2005
LuckY
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++).
• 08-21-2005
LuckY
Quote:

Originally Posted by swoopy
>If C++ follows C, then it may be you that is doing so.
C++ indeed follows C in this regard.

Would you mind quoting your source or a reference that might substantiate this claim?
• 08-21-2005
laserlight
Quote:

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.)
hmm... doesnt the C++ standard say that:
"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)
• 08-21-2005
ILoveVectors
speaking of the standard, id like a copy fo the standard
in pdf format, i wish it was free, it should be free, grr.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last