-2

"I am using two for loop one inside another.In the Innermost loop i am checking a 'if condition ' if condition will satisfy, the whole loop i mean both inner loop and outer loop should be stopped.But at the moment the below code can only stop the inner loop outer loop is not stopping.please give me solution."

for(int i=0; i<a_AllClothing.count;i++)
            {

                for (int j=0; j<tempAsset_array.count; j++) {

                    if([l_dressName rangeOfString:ImageName].location!=NSNotFound)
                    {
                        NSLog(@"Matched");

                        break;
                    }
                }
            }
DevMate
  • 19
  • 2
  • Are you 1st sem CS student? You should read "Let us C" or even +2 level Computer Science book. Use `condtion(s)` in outer loop, also you have the power of `goto, but don't use this frequently. – Anoop Vaidya Jun 15 '13 at 07:35
  • Sir i just wanted to know if there has any different technique rather than using a boolean value. – DevMate Jun 15 '13 at 07:39
  • In java you have labeled loop, in C / Obj-C you need to use `BOOL` or refactor your entire code to a method and in the innermost `if` you can use `return`. – Anoop Vaidya Jun 15 '13 at 07:42
  • Using `goto` to break out of a loop is perfectly valid. – CodaFi Jun 15 '13 at 07:56

2 Answers2

2

You can do the same with enumerationBlocks. This assumes that you have an array of arrays.

__block BOOL containsDress = NO;
[a_AllClothing enumerateObjectsUsingBlock:^(NSArray * clothes, NSUInteger idx, BOOL *firstStop) {
    [clothes enumerateObjectsUsingBlock:^(NSString * cloth, NSUInteger idx, BOOL *seconStop) {
        if ([cloth isEqualToString:l_dressName]) {
            *firstStop = YES;
            *seconStop = YES;
            containsDress = YES;
        }
    }];
}];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
1
BOOL isFound = NO;
for(int i=0; i<a_AllClothing.count && !isFound;i++)
            {

                for (int j=0; j<tempAsset_array.count; j++) {

                    if([l_dressName rangeOfString:ImageName].location!=NSNotFound)
                    {
                        NSLog(@"Matched");
                        isFound = YES;

                        break;
                    }
                }
            }
xqterry
  • 632
  • 4
  • 10