6

In Objective-C, is it possible to restart to the first iteration of a for loop? I don't want to do anything with my objects until I find a "good" object, at which point I want to go back and do stuff with every object up to that good object.

So,

bool someflag = false;
for (id object in array)
{
  if(object is good) 
  {
    someflag = true;
    //restart to first object in loop
  }
  if(someflag)
  {
    doStuffWithObject(object);
  }
}

Or is there a different/better way to do what I'm trying to do?

Obviously the easy way would be to just have two separate for loops -- so if I could get a second opinion telling me that's the best way, that's just what I'll shoot for. For some reason I have a feeling there's got to be a better way, though.

A O
  • 5,516
  • 3
  • 33
  • 68
  • 1
    Why wouldn't you want to do it with two loops? Would make sense to me to do it that way. – Fogmeister Jul 15 '14 at 13:01
  • @Fogmeister I have an inkling that there's a better way, but normally I would just use 2 for loops. Was just curious as to what people could come up with – A O Jul 15 '14 at 13:23
  • Ah, ok, no worries :D – Fogmeister Jul 15 '14 at 13:51
  • Don't forget to exit the loop when you get back to the marker object! Might even want to use the good object itself as the flag, so that you know when to break. (I realize this is pseudocode.) – jscs Jul 15 '14 at 19:41

4 Answers4

5

Not with fast enumeration, no (with the exception of goto), however if you use the indexed-access approach, you could:

NSUInteger count = [array count];
for (NSUInteger i = 0; i < count; i++)
{
    bool someflag = false;
    id object = array[i];
    if (isgood(object)) 
    {
        someflag = true;
        //restart to first object in loop
        i = 0;
    }
    if(someflag)
    {
        doStuffWithObject(object);
    }
}
Community
  • 1
  • 1
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • You should use `someflag` to make sure you don't keep resetting the loop repeatedly (or reorganise the if statements to prevent it) – Wain Jul 15 '14 at 13:03
  • 1
    This doesn't restart properly. You set `i` to 0 but then the `for` loop executes `i++` before going back to the top. So, the next iteration with be with `i` equal to 1. Also, without a `continue` statement, execution will proceed to the `if(someflag) ...` statement, and `someflag` is true, so it will execute `doStuffWithObject(object)` with the current object (before going back to the top. – Ken Thomases Mar 15 '15 at 06:48
3

If you really want to do this (rather than using a second loop, which is almost certainly more straightforward), you can use good old goto:

bool someflag = false;
restart:
for (id object in array)
{
  if(!someflag && object is good) 
  {
    someflag = true;
    goto restart;
  }
  if(someflag)
  {
    doStuffWithObject(object);
  }
}
Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
1

Consider using indexOfObjectPassingTest: to separate the logic in your code between the check (and getting the index) of the 'good' object and the subsequent processing.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

you can use a while loop for this

acutualElement = array.objectAtIndex[0];
int i = 0;
while(actualElemtnt =! null)
{
    if(...){
       do smth;
    } else {
       do smth different;
    }

    if (exit condition reached)
    {
       atualelement = null;
    }        
    else 
    {
       if (i < array.size) i++; //next element in array
       else i = 0; //first element in array
       actualElement = array.objectAtIndex[i];
    }
}
Starbax
  • 1,005
  • 2
  • 12
  • 32
  • ok, this produces an endless loop until exit condition reached. you wanted to go to 0 when smt. happens. this would work simular. I think @trojanfoe has the better answer ;) – Starbax Jul 15 '14 at 13:11