C语言 在 switch 语句中从一种情况跳转到默认情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7484660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jumping from one case to the default case in switch statement
提问by thetux4
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?
在像上面这样的 switch 语句中,我输入 case 'a',只有在其中的条件发生时才中断,否则我想跳转到默认情况。除了标签或 goto 之外,还有其他方法可以做到这一点吗?
回答by pmg
gotoFor The Win
goto为了胜利
switch (ch) {
case 'a':
if (1) goto LINE96532;
break;
case 'b':
if (1) goto LINE96532;
break;
LINE96532:
default:
//
break;
}
回答by Diego Sevilla
Just reorder the cases so that that case is the last:
只需重新排序案例,以便该案例是最后一个:
switch(ch){
case 'b':
//..
case 'c':
//..
case '_':
//...
case 'a':
//do something, condition does not match so go to default case
if (condition)
break;
//don't break in here, and don't allow fall through to other cases.
default:
//
break;
}
回答by mouviciel
If the condition doesn't depend on cases, why put it inside?
如果条件不依赖于案例,为什么要把它放在里面?
if (!condition){
// do default
}else{
switch(ch){
case 'a':
// do a
break;
...
}
}
回答by Kaos
Refactor your code:
重构你的代码:
int test_char(char ch)
{
switch(ch) {
case 'a': if (condition) return 0; break;
case 'b': // ...
default: return -1;
}
return 1;
}
...
// defaults processing switch
switch(test_char(ch)) {
case 0: break; // condition met
case 1: // default processing
default: // case not handled by test_char
}
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.
这也增加了可以灵活地测试多类默认处理的好处。假设您有一组共享一些通用逻辑的字符 [c, d, e, f]。对于这些情况,只需从 test_char() 返回 2(可能在测试了某些条件之后),并将 case 2: 处理程序添加到默认处理 switch 语句中。
回答by Andrew
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:
如果您绝对不想使用标签,并且希望将案例保持在当前顺序,那么您可以在案例 'a' 之后继续,然后检查是否在每个案例的开头查看 if(ch != 'a')后续情况,只有在条件为真时才执行语句:
switch(ch){
case 'a':
// do something
case 'b':
if(ch != 'a') {
//do something
}
//repeat for each subsequent case
default:
//do something
break;
}
This is probably not the most efficient way to solve your problem, but it should accomplish what you want.
这可能不是解决问题的最有效方法,但它应该可以完成您想要的。
回答by Taha Paksu
Here's what I did:
这是我所做的:
char ucResult = 1;
switch(ch){
case 'a':
if(ucResult){
// do something
if(error) ucResult = 0;
}
case 'b':
if(ucResult){
// do something
if(error) ucResult = 0;
}
case 'c':
if(ucResult){
// do something
if(error) ucResult = 0;
}
case '_':
if(ucResult){
// do something
if(error) ucResult = 0;
}
default:
//
break;
}
With this structure, you can switch to default case from any previous cases. Handy for breaking outer loops too.
使用此结构,您可以从以前的任何案例切换到默认案例。也可以方便地打破外循环。
回答by Jason
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 switchcases, and if that flag is set, then do a default operation. For instance:
如果您必须首先使用 switch 语句,因为您要检查的条件取决于案例(或者必须先评估案例才能检查条件),只需在switch案例中设置一个标志,如果该标志设置,然后做一个默认的操作。例如:
int default_true = 0;
switch (case_value)
{
case 'a': /* if the condition is true, set the default_true flag */
case 'b': /* if the condition is true, set the default_true flag */
//...
default: default_flag = 1; // set the default_true flag to true
}
if (default_flag)
{
//place your "default" code here rather than inside the switch statement
//this prevents code reduplication
}
回答by Minh Tran
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.
我希望我的解决方案能回答你的问题。只需让案例一直执行(从匹配案例开始),但使用条件来禁止后续案例运行。
typedef enum boolean
{
FALSE = 0, TRUE = 1
} bool;
void pstuff(char input)
{
bool _skip = FALSE;
switch(input)
{
case 'a':
printf("Case a.");
_skip = TRUE;
case 'b':
if(!_skip)
{
printf("Case b.");
_skip = TRUE;
}
case 'c':
if(!_skip)
{
printf("Case c.");
_skip = TRUE;
}
//...
default:
printf("Done!\n"); //Always gets printed.
}
}
回答by user11852835
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.
嗯,这个帖子真的很老了,但要回答大家:你可以简单地写'goto default;' 并且您将直接跳转到默认情况,没有任何问题。
Example:
例子:
switch (value)
{
case value1:
// do something;
break;
case value2:
// do something
break;
.
.
.
.
case value20:
// do something
**goto default;**
.
.
case valueN:
// do something
break;
default:
// do something
break;
}
回答by Ebrahim Hassan
jumping to default
跳转到默认
- use an empty case label just above default ( with an obvious name )
- Use
goto case "case-labelname"to jump to and then fall thru to default..
- 使用略高于默认值的空案例标签(具有明显的名称)
- 使用
goto case "case-labelname"跳转到,然后下降到直通默认..
example
例子
switch (VAR)
{
case "a":
// dO STUFF !
if(COND)
goto case "DFLT";
// dO STUFF !
break;
case "B":
// dO STUFF !
break;
case "C":
// dO STUFF !
IF(COND)
goto case "DFLT";
break;
case "DFLT":
default:
// dO DEFAULT
break;
}

