![js打破循环](https://img-blog.csdnimg.cn/img_convert/77d54ff02f78a7dcef918fd91d923264.png)
js打破循环
The cert guide is maybe not 100% clear on the question of breaking out of a loop, but you can break
and continue
in all types of loops - for
, while
and foreach
(the latter wasn't mentioned in the loops section of the book).
CERT的指导,也许是一个环路上的突破的问题不是100%清楚,但你可以break
,并continue
在所有类型的循环- for
, while
和foreach
(后者在书的环路部分没有提到)。
About the continue when in nested loops. Well, it is true that you can do < ?php for ($i = 0; $i < 10; $i++){ for ($j = 0; $j < 10; $j++){ if ($i % 2) { continue 2; } } // this line will be executed 5 times // and will write 0 2 4 6 8 echo $i; } ?> Or you can just continue
or continue 1
, rather than continue 2
, like this < ?php for ($i = 0; $i < 10; $i++){ for ($j = 0; $j < 10; $j++){ if ($i % 2) { continue; } } // this line will be executed 10 times // and will write 0 1 2 3 4 5 6 7 8 9 echo $i; } ?>
关于嵌套循环中的continue。 好吧,的确可以做到<?php for($ i = 0; $ i <10; $ i ++){for($ j = 0; $ j <10; $ j ++){if($ i%2 ){继续2; }} //此行将执行5次// //写入0 2 4 6 8 echo $ i; }?>或者您可以continue
或continue 1
,而不是continue 2
,例如<?php for($ i = 0; $ i <10; $ i ++){for($ j = 0; $ j <10; $ j ++){如果($ i%2){继续; }} //此行将被执行10次//并将写入0 1 2 3 4 5 6 7 8 9 echo $ i; }?>
So yes, it is possible, but to me, these continue 2
, continue 3
and so on can only cause confusion and make the code harder to follow. If I find myself in situation where I write continue 2
, I would seriously think about restructuring this particular piece of logic.
所以是的,这是可能的,但是对我来说,这些continue 2
, continue 3
等等只会造成混乱并使代码难以遵循。 如果我发现自己在写continue 2
情况下,我会认真考虑重组这一特定逻辑。
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息
js打破循环