Cleaning a text field How can I optimize the if condition in this snippet? The only one difference is &amp;&amp; [self isCurrentPosition:i]. How can I make it a single if, including the condition val?

Note: self is a category of NSArray.

- (void) cleanTextfieldExcluding:(int)current checkPosition:(BOOL)val {
  for ( int i=0; i&lt;[self count]; i++ ) {

    // -----------
    if ( val ) {  // this IF block is very bad
      if ( i != current &amp;&amp; [self isCurrentPosition:i] )
        [self replaceObjectAtIndex:i withObject:@""];
    } else {
      if ( i != current ) 
        [self replaceObjectAtIndex:i withObject:@""];
    }
    // -----------

  }
}
the (val · ¬cur · pos) | (¬val · ¬cur) = ¬cur (val·pos | ¬val) = ¬cur (pos | ¬val). So, the condition is 

if ( (i != current) &amp;&amp; (!val || [self isCurrentPosition:i]) ) {
        [self replaceObjectAtIndex:i withObject:@""];
    }


You just must practise factoring out the common subexpressions and subprograms and learn how to use the Carnot maps for boolean minimization.