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

This is a discussion on Comparison Operator Comparing Two Values ('y' || 'x' == blah) within the C++ Programming forums, part of the General Programming Boards category; I'm doing the following: Code: if (('x' || 'y') == someChar) { //statements; } This doesn't evalulate as true even ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    16

    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?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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'))
    "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

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    16
    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?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    '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.

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    if(('x' == someChar) || ('y' == someChar))
    {
         //do something
    }
    else
    {
         //do something else
    }

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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'."

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    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.
    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.*

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    16
    This is making some sense now. What do you all mean by a "non-zero" value?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    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.*

  10. #10
    Banned
    Join Date
    Jun 2005
    Posts
    594
    any value that is not 0 (zero)
    a few non-zero would be

    Code:
    123456789-=~`!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?qwertyuiop[]\asdfghjkl;'zxcvbnm,./

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >If C++ follows C, then it may be you that is doing so.
    C++ indeed follows C in this regard.

  12. #12
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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++).

  13. #13
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,163
    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)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Banned
    Join Date
    Jun 2005
    Posts
    594
    speaking of the standard, id like a copy fo the standard
    in pdf format, i wish it was free, it should be free, grr.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Need help with my code
    By brietje698 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2007, 03:54 PM
  3. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 05:38 PM
  4. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21