if statement

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

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #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
    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. thread starter macrumors 68000

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

    Thanks!

    -Lars
     

Share This Page