if statement

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

  1. macrumors 65816

    Joined:
    Sep 8, 2006
    Messages:
    1,494
    #1
    Is there a proper side in an IF statements expression to have the control value tested against another value (I hope I said that right).
    Code:
    -(void)mostRolledNumMethod{
        for (int i = 1; i <= 100; i++) {
            if (mostRolledNumber[i] < highestRollSoFar) {
                highestRollSoFar = mostRolledNumber[i];
            }
        }
    So should the 'mostRolledNumber' be to the right, left or it does not matter?

    -Lars
     
  2. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,827
    Location:
    New England
    #2
    Depends what's in the parentheses.

    In this case (a>b) is very different from (b>a). :p

    In C the if just looks at the return value of the expression and checks if it is zero (a.k.a FALSE) or non-zero (a.k.a. TRUE).

    EDIT: Your code as posted will go through the array mostRolledNumber and update highestRollSoFar when it finds one LESS than highestRollSoFar, which may not be what you want since that tends to be a minimum finding routine.

    B
     
  3. macrumors 65816

    Joined:
    Sep 8, 2006
    Messages:
    1,494
    #3
    Yep, I caught that the hard way :)

    Thanks!

    -Lars
     

Share This Page