11

I have a nested loop like so:

while(1){

    while($something){
        break & continue;
    }
    // More stuff that I don't want to process in this situation
}

I want to break out of the 2nd while loop and continue the 1st loop from the start (without finishing the 1st loop). Is this possible without using variables?

nick
  • 2,743
  • 4
  • 31
  • 39
  • I have IF/THEN statements inside the 2nd loop that determine whether i should continue, break, or break AND continue. I could use variables to determine that, but is it possible without them is what i'm asking – nick Sep 15 '12 at 04:38
  • This sounds more like your logic might benefit from a bit more structure than anything else; break and continue are typically last resorts to begin with. – Brad Koch Sep 15 '12 at 04:43
  • I don't like too many nested IF/THENs in my daemons. There's a reason why I need to use breaks and continues. – nick Sep 15 '12 at 04:46
  • Another suggestion would be to add some functional decomposition. It'll be easier to maintain that way too. – Brad Koch Sep 15 '12 at 04:49

3 Answers3

12

You can continue 2; in your inner loop to continue the outer loop from the beginning.

http://php.net/manual/en/control-structures.continue.php

continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.

jimp
  • 16,999
  • 3
  • 27
  • 36
1

Just use 'break' in inner loop. It will break out of the inner loop and the outer loop will continue.

Praveen
  • 522
  • 1
  • 8
  • 17
  • I have more stuff after the 2nd while loop that I don't want to process. There's a reason why I need it this way. – nick Sep 15 '12 at 04:36
  • Oh I see. That was missing in the code you posted. Did you look into numbered break and continue? See this - http://stackoverflow.com/questions/5167561/loop-break-continue-break-2-continue-2-in-php – Praveen Sep 15 '12 at 04:45
0

After the inner while, can you not just use an if statement to execute the continue?

while(1){

    while($something){
        break;
    }

    if (!$something) {
       continue;
    }

    // More stuff that I don't want to process in this situation
}
endyourif
  • 2,186
  • 19
  • 33
  • of course i can, and that's what i'm probably going to do. my question was is this possible to do without additional variables – nick Sep 15 '12 at 04:48