[SOLVED] Is this not valid? (Last question I promise!)

Discussion in 'Mac Programming' started by chrono1081, Oct 26, 2011.

  1. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #1
    Sorry for my second post in such a short time, but I just wondered if there is something wrong here.

    I was doing an exercise from a book which is pretty simple but it wasn't working correctly. The culprit was a do-while loop. A shortened version of the code is this:

    Code:
    
    char operator;
    
    do {
             //do stuff here regarding the value of operator
    
    }while ( (operator != 'e') || (operator != 'E') )
    
    For some reason, the loop never exits even though operator gets set to e or E. The code works perfectly fine if I change the while section to one of the following:

    Code:
    while(operator != 'e') //here I only put in a lowercase e and it works
    while(operator != 'E') //here I only put in an uppercase E and it works
    while(toupper(operator) != 'E') //again, this works
    
    Am I unable to make comparisons in the do-while loops arguments? Since operator is equal to 'e' OR 'E' the loop should break, but it doesn't unless I remove the comparison.
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    Fill in the boolean values of the sub-expressions:

    e: (operator != 'e') -> NO
    (operator != 'E') -> YES
    NO || YES -> YES

    E: (operator != 'e') -> YES
    (operator != 'E') -> NO
    YES || NO -> YES

    You need && instead of ||.
     
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    I think you want && not ||.

    Code:
     op | A = (op != 'e') | B = (op != 'E') | A && B | A || B
    ----+-----------------+-----------------+--------+--------
    'E' | True            | False           | False  | True
    'e' | False           | True            | False  | True
    'q' | True            | True            | True   | True
    
     
  4. chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #4
    Ohhhh I'm an idiot. Thanks guys!

    Its time for bed. I've been goofing up non-stop for the past hour and a half :p
     

Share This Page