我注意到在 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循环的末尾):
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
旨在跳转到for
循环的结尾。 - user743382