‘打破’从开关,然后“继续”循环中
是否可以从开关中断然后继续循环?
例如:
$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
foreach($letters as $letter) {
foreach($numbers as $number) {
switch($letter) {
case 'd':
// So here I want to 'break;' out of the switch, 'break;' out of the
// $numbers loop, and then 'continue;' in the $letters loop.
break;
}
}
// Stuff that should be done if the 'letter' is not 'd'.
}
这可以做到吗?语法是什么?
Is it possible to break from a switch and then continue in a loop?
For example:
$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
foreach($letters as $letter) {
foreach($numbers as $number) {
switch($letter) {
case 'd':
// So here I want to 'break;' out of the switch, 'break;' out of the
// $numbers loop, and then 'continue;' in the $letters loop.
break;
}
}
// Stuff that should be done if the 'letter' is not 'd'.
}
Can this be done, and what would the syntax be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想使用
break n
澄清后,看起来您想要
继续 2;
You want to use
break n
After clarification, looks like you want
continue 2;
使用
继续2
而不是break
。Instead of
break
, usecontinue 2
.我知道这是一个严重的死灵,但是……当我从谷歌来到这里时,我想我可以避免其他人的困惑。
如果他的意思是从开关中退出并只是结束数字循环,那么
break 2;
就可以了。continue 2;
只会继续数字的循环并继续迭代它,以便每次都继续
。因此,正确的答案应该是
continue 3;
。通过 文档中的评论继续基本上会转到结构的末尾,对于 switch 来说就是这样(感觉与break相同),对于循环它会在下一次迭代中拾取。
请参阅:http://codepad.viper-7.com/dGPpeZ
上述情况的示例 n/ a:
结果:
continue 2;
不仅处理字母 d 的字母循环,甚至还处理数字循环的其余部分(请注意,$i
既递增又打印f)之后。 (这可能是理想的,也可能不是理想的......)希望能帮助其他首先到达这里的人。
I know this is a serious necro but... as I arrived here from Google figured I'd save others the confusion.
If he meant breaking out from the switch and just ending the number's loop, then
break 2;
would've been fine.continue 2;
would just continue the number's loop and keep iterating through it just to becontinue
'd each time.Ergo, the correct answer ought to be
continue 3;
.Going by a comment in the docs continue basically goes to the end of the structure, for switch that's that (will feel the same as break), for loops it'd pick up on the next iteration.
See: http://codepad.viper-7.com/dGPpeZ
Example in case above n/a:
Results:
continue 2;
not only processes the letter's loop for the letter d but even processes the rest of the number's loop (note that$i
is both incremented and printed after f). (Which may or may not be desirable...)Hope that helps anyone else that winds up here first.