Char Comparison Logic error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chazzy69
    New Member
    • Sep 2007
    • 196

    #1

    Char Comparison Logic error

    Hello i seem to have trouble with comparing two chars

    e.g.
    This works if
    Code:
    (char == 'z')
    but when i try if
    Code:
    (char != 'z')
    it always returns false.

    Heres the actual line of code-
    Code:
    while (var_name !=('a' || 'b' || 'c' || 'd' || 'e' || 'f'')  ) {
    code goes here
    }
    The problem is that even if var_name is equal to one of those values it loops infinitley.

    Any help in this matter is greatly appreciated
  • chazzy69
    New Member
    • Sep 2007
    • 196

    #2
    Actually i did some more checking it seems rather to be a error with the or statement -

    Code:
    var_name !=('a' || 'A');
    but
    Code:
    var_name !=('a');
    works

    Comment

    • chazzy69
      New Member
      • Sep 2007
      • 196

      #3
      No need to reply i have got around the problem,

      by using an if flag within the a while statement,

      Thanks anyways

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        ('a' || 'A') will have the value 1, || is a logical OR and outputs 0 or 1. It only outputs 0 if both sides of the operand are 0 so ('a' || 'A') = 1.

        Therefore

        Code:
        var_name !=('a' || 'A')
        is equivalent to
        Code:
        var_name != 1
        which is, I suspect, not what you wanted. To test to see if var_name is not 'a' and not 'A' you need this expression
        Code:
        var_name != 'a' && var_name != 'A'

        Comment

        Working...