<分区>
在我工作的许多项目中,我发现我已经嵌套了采用这种通用形式的 for
循环(我将重点关注 PHP 实现,但也适用于 javascript):
$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;
}
重申一下,如果在最内层循环中发现无效状态,则所有循环都应中断。但据我所知,要中断所有循环,我必须设置一个变量(在本例中为 $invalidState
),并在所有外部循环中检查它,如果为真,则中断.
是否存在任何类型的“ super 中断”,如果在最内层循环中满足条件,则所有外层循环都将被中断?