1

On many of the projects on which I work, I find that I have nested for loops (I will focus on PHP implementation but applies to javascript as well) that take this generic form:

$array1 = array(........);
$count1 = count($array1);

$invalidState = false;//An invalid state will be looked for in the innermost loop, and if found, break all the loops

for($i = 0; $i < $count1; $i++) {
    //parsing, then another loop
    $array2 = explode("needle", $array1[$i]);
    $count2 = count($array2);
    for($j = 0; $j < $count2; $j++) {
        //parsing, then sometimes even another another loop
        $array3 = explode("different-needle", $array2[$j]);
        $count3 = count($array3);
        for($k = 0; $k < $count3; $k++) {
            //check for an invalid state in $array3[$k], and break if invalid state = true; 
        }
        if ($invalidState) break;
    }
    if ($invalidState) break;
}

To reiterate, if an invalid state is found in the innermost loop, then all the loops should break. But as far as I am aware, to break from all the loops, I must set a variable (in this case $invalidState), and check this in all the outer loops, and if true, then break.

Is there any kind of "super-break" where if a condition is met in the innermost loop, all outer loops will be broken?

5 Answers5

6

Yes, try this:

for($i = 0; $i < $count1; $i++) {
    //parsing, then another loop
    $array2 = explode("needle", $array1[$i]);
    $count2 = count($array2);
    for($j = 0; $j < $count2; $j++) {
        //parsing, then sometimes even another another loop
        $array3 = explode("different-needle", $array2[$j]);
        $count3 = count($array3);
        for($k = 0; $k < $count3; $k++) {
            break 3; 

    }
    if ($invalidState) break;
}
if ($invalidState) break;
}

When you write break n you are breaking n nested loops. However, you can't break $foo, because this ability has been recently removed:

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

Luis González
  • 3,199
  • 26
  • 43
  • 2
    This should be considered as the answer, since it truly give a response, not a workaround (return) – Alexandre Beaudet Nov 24 '15 at 13:28
  • 1
    Actually, `break n` was removed from PHP since PHP 5.4 (http://php.net/manual/en/control-structures.break.php) so this example will throw syntax error on PHP 5.4+. – martin Nov 24 '15 at 13:48
  • @Martin From the link you gave me: `5.4.0 - Removed the ability to pass in variables (e.g., $num = 2; break $num;) as the numerical argument.` but it seems that `break 3` still working – Luis González Nov 24 '15 at 13:50
  • Ou, you're right, its related to only `break $var` syntax. – martin Nov 24 '15 at 13:57
  • @Martin thank you for the observation, I learnt something new! – Luis González Nov 24 '15 at 14:00
3

Probably most easily just wrap the loop in a function and when you want to use "super-break" just use return.

martin
  • 93,354
  • 25
  • 191
  • 226
2

I would suggest making your outer loop its own function, and returning explicitly in the center of the loop to hard break from all the nested loops.

Devon Parsons
  • 1,234
  • 14
  • 23
2

You should consider this:

break 3; //Breaks 3 levels, so breaks outermost foreach
Atif Tariq
  • 2,650
  • 27
  • 34
1

You can use return for this purpose

Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50