switch 语句中从一种情况跳转到默认情况
switch(ch){
case 'a':
//do something, condition does not match so go to default case
//don't break in here, and don't allow fall through to other cases.
case 'b':
//..
case 'c':
//..
case '_':
//...
default:
//
break;
}
在像上面这样的 switch 语句中,我输入 case 'a',只有当其中的条件发生时我才会中断,否则我想跳转到默认情况。除了标签或 goto 之外,还有其他方法可以做到这一点吗?
switch(ch){
case 'a':
//do something, condition does not match so go to default case
//don't break in here, and don't allow fall through to other cases.
case 'b':
//..
case 'c':
//..
case '_':
//...
default:
//
break;
}
In a switch statement like above one I enter case 'a', I break only if the condition inside it occurs, otherwise I want to jump to default case. Is there any other way of doing this rather than labels or gotos?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
goto
为了胜利goto
For The Win只需重新排序案例,使该案例成为最后一个:
Just reorder the cases so that that case is the last:
如果条件不取决于情况,为什么要把它放在里面呢?
If the condition doesn't depend on cases, why put it inside?
重构代码:
这还增加了灵活测试多个默认处理类的好处。假设您有一组共享一些共同逻辑的字符 [c, d, e, f]。对于这些情况,只需从 test_char() 返回 2(可能在测试了某些条件之后),并将 case 2: 处理程序添加到默认处理 switch 语句中。
Refactor your code:
This also adds the benefit of being flexible to test for multiple classes of default processing. Say you have a group of chars [c, d, e, f] which share some common logic. Simply return 2 from test_char() for these cases (possibly after some conditions has been tested), and add a case 2: handler to the default processing switch statement.
我不确定这是否是最好的答案,但事情是这样的:
如果您绝对不想使用标签,并且希望将案例保持在当前顺序,那么您可以在案例“a”之后继续,然后check so see if(ch != 'a') 在每个后续情况的开头,仅在条件为真时执行语句:
这可能不是解决问题的最有效方法,但它应该完成您想要的。
I'm not sure if thes is the best answer, but here it goes:
If you absolutely do not want to use labels, and you want to keep the cases in their current order, then you could continue after case 'a' and then check so see if(ch != 'a') at the beginning of each subsequent case, only executing the statement if the condition is true:
This is probably not the most efficient way to solve your problem, but it should accomplish what you want.
如果您必须首先使用 switch 语句,因为您要检查的条件取决于大小写(或者必须先评估大小写,然后才能检查条件),只需在
switch 情况下,如果设置了该标志,则执行默认操作。例如:
If you must have the switch statements first because the condition you're checking for depends on the case (or the case has to be evaluated first before you can check on the condition), simply set a flag inside your
switch
cases, and if that flag is set, then do a default operation. For instance:这就是我所做的:
使用此结构,您可以从任何以前的情况切换到默认情况。也可以方便地打破外循环。
Here's what I did:
With this structure, you can switch to default case from any previous cases. Handy for breaking outer loops too.
我希望我的解决方案能回答您的问题。只需让案例一直执行(从匹配的案例开始),但使用条件来禁止后续案例运行。
I hope my solution answers your question. Simply let the cases follow through all the way (beginning with the matching case) but use a condition to disable subsequent cases from running.
好吧,帖子确实很老了,但还是回答一下大家:
你可以简单地写“goto default;”你会直接跳转到默认情况,没有任何问题。
例子:
Well, the post is really old but to answer everyone:
you can simple write 'goto default;' and you will directly jump to the default case without any problems.
Example: