【问题标题】:'break' from a switch, then 'continue' in a loop从开关“中断”,然后在循环中“继续”
【发布时间】:2011-11-20 22:55:02
【问题描述】:

是否可以从 switch 中中断然后继续循环?

例如:

$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'.

}

这可以做到吗,语法是什么?

【问题讨论】:

  • 太棒了,我从来不知道这一点,我花了很长时间才意识到这是问题所在,开关的中断会影响外部 foreach:(来自 php 站点)“请注意,与其他一些语言不同,继续语句适用于 switch,作用类似于 break。如果循环内有 switch,并希望继续到外循环的下一次迭代,请使用 continue 2。"

标签: php control-structure


【解决方案1】:

你想使用break n

break 2;

经过澄清,貌似你要continue 2;

【讨论】:

  • 如果我要使用“break 2;”在 $numbers 循环之后它不会继续执行代码,但仍在 $letters 循环内吗?
  • @Drew:我没有意识到你的意思是 continue 作为一个技术术语。
【解决方案2】:

使用continue 2 代替break

【讨论】:

  • 程序将回到数字循环的开头。这是php独有的。你可以在这里查看=>php.net/manual/en/control-structures.switch.php
  • " 注意:请注意,与其他一些语言不同,continue 语句适用于 switch,其作用类似于 break。如果循环内有 switch,并希望继续外循环的下一次迭代, 使用 continue 2. "
【解决方案3】:

我知道这是一个严重的死灵,但是...当我从 Google 来到这里时,我想我可以避免其他人的困惑。

如果他的意思是从交换机中跳出并结束数字的循环,那么break 2; 就可以了。 continue 2; 只会继续数字的循环并不断迭代它,以便每次都成为continue

所以,正确答案应该是continue 3;

通过a comment in the docs continue 基本上到结构的末尾,对于 switch 就是这样(感觉与 break 相同),对于循环它会在下一次迭代中拾取。

见:http://codepad.viper-7.com/dGPpeZ

以上情况不适用的示例:

<?php
    echo "Hello, World!<pre>";

$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');

$i = 0;
foreach($letters as $letter) {
    ++$i;
    echo $letter . PHP_EOL;
    foreach($numbers as $number) {
        ++$i;
        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.
              continue 3; // go to the end of this switch, numbers loop iteration, letters loop iteration
            break;
           case 'f':
            continue 2; // skip to the end of the switch control AND the current iteration of the number's loop, but still process the letter's loop
            break;
           case 'h':
            // would be more appropriate to break the number's loop
            break 2;

        }
        // Still in the number's loop
        echo " $number ";
    }


    // Stuff that should be done if the 'letter' is not 'd'.
    echo " $i " . PHP_EOL;

}

结果:

Hello, World!
a
 1  2  3  4  5  6  7  8  9  0  11 
b
 1  2  3  4  5  6  7  8  9  0  22 
c
 1  2  3  4  5  6  7  8  9  0  33 
d
e
 1  2  3  4  5  6  7  8  9  0  46 
f
 57 
g
 1  2  3  4  5  6  7  8  9  0  68 
h
 70 
i
 1  2  3  4  5  6  7  8  9  0  81 

continue 2; 不仅处理字母 d 的字母循环,还处理数字循环的其余部分(请注意,$i 在 f 之后增加和打印)。 (这可能是可取的,也可能不是可取的......)

希望能帮助到这里的其他人。

【讨论】:

    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2010-09-05
    • 2020-06-09
    相关资源
    最近更新 更多