Should I ever use continue inside a switch statement?

不想你离开。 提交于 2019-12-21 03:09:11

问题


I noticed you can indeed use the continue keyword in a switch statement, but on PHP it doesn't do what I expected.

If it fails with PHP, who knows how many other languages it fails too? If I switch between languages a lot, this can be a problem if the code doesn't behave like I expect it to behave.

Should I just avoid using continue in a switch statement then?

PHP (5.2.17) fails:

for($p = 0; $p < 8; $p++){
    switch($p){
        case 5:
            print"($p)";
            continue;
            print"*"; // just for testing...
        break;
        case 6:
            print"($p)";
            continue;
            print"*";
        break;
    }
    print"$p\r\n";
}
/*
Output:
0
1
2
3
4
(5)5
(6)6
7
*/

C++ seems to work as expected (jumps to end of for loop):

for(int p = 0; p < 8; p++){
    switch(p){
        case 5:
            cout << "(" << p << ")";
            continue;
            cout << "*"; // just for testing...
        break;
        case 6:
            cout << "(" << p << ")";
            continue;
            cout << "*";
        break;
    }
    cout << p << "\r\n";
}
/*
Output:
0
1
2
3
4
(5)(6)7
*/

回答1:


Unfortunately, continue and break are the same in PHP when using switch statements.

Don't let PHP ruin your experiences in other languages, and continue to use continue where you need it!




回答2:


Try using continue 2 to continue to the next iteration of the loop surrounding the switch statement.

EDIT:

    $foo = 'Hello';

    for ($p = 0; $p < 8; $p++) {
         switch($p) {
             case 3:
                 if ($foo === 'Hello') {
                     echo $foo;
                     break;
                 } else {
                      continue 2;
                 }

             default:
                 echo "Sleeping...<br>";
                 continue 2;

         }

         echo "World!";
         break;
    }

//This will print: Sleeping... Sleeping... Sleeping... Hello World!




回答3:


The documentation for the PHP continue statement makes this clear:

Note: Note that in PHP the switch statement is considered a looping structure for the purposes of continue.

You should know that different languages give the same keywords subtly different meanings, and not assume that PHP continue behaves the same as C++ continue.

If continue makes sense in a PHP switch where it wouldn't work in C++, do use it.

If continue makes sense in a C++ switch where it wouldn't work in PHP, do use it.




回答4:


As is warned in the PHP Manual:

"Note that in PHP the switch statement is considered a looping structure for the purposes of continue."

So the use of continue will break out of the switch statement, not the for loop. The perils of assuming similar syntax across languages.




回答5:


It is important to note that continue and break do not behave the same when the switch statement is nested within a loop. If you are using a switch statement to evaluate something, and want to move onto the next item within the loop if the condition is met, you should use continue 2. Using break 2 in this case will break out of the entire for loop, which may not be the desired action.




回答6:


In C and C++, the switch statement is only a fancy combination of if/else if and labels/goto, so using continue inside switch is okay. But as you noticed it doesn't do what you expect it to in other languages that are similar to C or C++. That's because they are only similar when it comes to syntax, for semantic rules they are very different beasts. So a thing that works in one language will most definitely not work in another even if the languages look similar.




回答7:


Using continue inside a C++ switch / case construct that is embedded in a loop is perfectly OK. You shouldn't restrict your style in C++ just because of misbehavior occurring in other programming languages.



来源:https://stackoverflow.com/questions/12349826/should-i-ever-use-continue-inside-a-switch-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!