PDA

View Full Version : if statement




larswik
Jun 28, 2011, 05:05 PM
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).
-(void)mostRolledNumMethod{
for (int i = 1; i <= 100; i++) {
if (mostRolledNumber[i] < highestRollSoFar) {
highestRollSoFar = mostRolledNumber[i];
}
}

So should the 'mostRolledNumber[i]' be to the right, left or it does not matter?

-Lars



balamw
Jun 28, 2011, 05:10 PM
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

larswik
Jun 28, 2011, 05:43 PM
Yep, I caught that the hard way :)

Thanks!

-Lars