&& ||

Discussion in 'Mac Programming' started by larswik, Jun 28, 2011.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    These AND & OR are driving me nuts! They are always backwards from what I think.

    I have this code in my dice program
    Code:
    do {
            dieRolls[0] = arc4random() % 100 + 1;
        } while ((dieRolls[0] > [numberNoMoreThen intValue]) || (dieRolls[0] < [numberNoLessThen intValue]));
    
    I read in a book once that said think of these to operators like this

    1) && - You can go to the movies if your room is clean AND dishes are done. Both have to be true to return a true.

    2) || - You can go to the movies if your room is clean OR dishes are done. One or the other needs to be true to return a true.

    This code works, but I first used the && operator. I scrapped the popUpButton idea and went with another slider to set the min low and max high values of the roll.

    It would seem that out of a total max range of 1 to 100. I set the low to 20 and the high to 80 that if the roll was above 80 AND bellow 20 it would re-roll the dice until it was in-range. But && does not work, but the || works fine. I could set min to 49 and max to 50 and I would get one or the other value when I press the roll dice button.

    Why, it seems so backwards? This logic is not logical.
     
  2. macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #2
    Think of it this way - a number cannot be both above 80 AND below 20. Try to think of a number which is both above 80 AND below 20. It can't be done.

    So if a number is above 80 OR the number is below 20, it is out of range.
     
  3. macrumors member

    AustinZ

    Joined:
    Aug 6, 2008
    #3
    It's logically impossible for the value of a number to be both above 80 and below 20 at the same time.

    Think of it this way: I decided how to spend my night. Either I did homework, I went to see a movie, or I went to the bar with friends. (Only one can be true.)

    I can say that 'if I went to see a movie' || 'if I went to the bar with friends' then 'I failed my test'.

    But I can't say that 'if I went to see a movie' && 'if I went to the bar with friends', because then I'd be in two places at the same time and it would be logically impossible.

    Likewise, the number is either below 20, between 20 and 80, or greater than 80, and only one of those three options can be true.
     
  4. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    It's not really && vs.|| that are giving you the grief here. It's really while vs. until. While and until are opposite to each other.

    You're thinking "loop until so-and-so is true or so-and-so is true". But what you need to code is "loop while so-and-so is false and so-and-so is false". (Boolean algebra says !(A || B) == !A && !B).

    Objective-C doesn't have until. So always think in terms of while. "I want this code to keep repeating while ...."
     
  5. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    ahhhh. Now I see the logic. That 1 number can not be 2 things, above and bellow at the same time. I see that now.

    I managed to crash my dice program when I made the low number higher then the high number an vice versa. opps. I think it went into an infinite rolling loop.:eek:

    -Lars
     
  6. macrumors newbie

    Joined:
    Aug 13, 2007
    #6
    Yep, that would do it - if you're testing for a number that is (more than 20) OR (less than 80) then your while test will always evaluate to TRUE, because all numbers are always either more than 20, or less than 80.

    So with your while clause always returning TRUE, the DO loop becomes an infinite loop.
     
  7. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    I would suggest that at this point, you stuff the loop condition into a temporary variable (or more if needed) so that you can watch and debug it separately.

    IMHO this would have saved you a lot if grief in both this thread and your last one.

    B
     
  8. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    Yep. Last night I saw that and changed the whole thing to an IF statement to avoid the infinite loop that dis happen. This is all a learning process. Thanks.
     

Share This Page