我注意到您确实可以在switch语句中使用continue
关键字,但在php中,它并没有达到我预期的效果。
如果用PHP失败了,谁知道还有多少其他语言也失败了?如果我经常在语言之间切换,如果代码的行为不像我期望的那样,这可能是一个问题。
那么,我应该避免在switch语句中使用continue
php(5.2.17)失败:
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++似乎按预期工作(跳到循环的末尾):
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
*/
最佳答案:
不幸的是,当使用continue
语句时,break
和PHP
在switch
中是相同的。
不要让PHP
破坏您在其他语言中的体验,并在需要的地方继续使用continue
!