在switch语句中从一个case跳转到默认大小写

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’,只有当它内部的条件发生时才会中断,否则我想跳转到默认情况。 有没有其他方式这样做而不是标签或者gotos?

goto胜利

 switch (ch) { case 'a': if (1) goto LINE96532; break; case 'b': if (1) goto LINE96532; break; LINE96532: default: // break; } 

只需重新排序案例,以便最后一个案例:

 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; } 

如果病情不依赖于病例,为什么要把它放进去?

 if (!condition){ // do default }else{ switch(ch){ case 'a': // do a break; ... } } 

重构你的代码:

 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 } 

这还增加了灵活地测试多类默认处理的好处。 假设你有一组共享一些共同逻辑的字符[c,d,e,f]。 对于这些情况(可能在测试了一些条件之后),只需从test_char()返回2,并将case 2:handler添加到缺省处理switch语句中。

我不确定这是否是最好的答案,但在这里:

如果你绝对不想使用标签,并且你想要保持当前顺序的情况,那么你可以在案例’a’之后继续,然后检查,看看是否(ch!=’a’)在每个开头后续情况,只有在条件为真时才执行语句:

 switch(ch){ case 'a': // do something case 'b': if(ch != 'a') { //do something } //repeat for each subsequent case default: //do something break; } 

这可能不是解决问题的最有效方法,但它应该完成你想要的。

如果您必须首先使用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 } 

这是我做的:

 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; } 

使用此结构,您可以从以前的任何情况切换到默认情况。 方便打破外环。

我希望我的解决方案能回答你的问题 只需让案例一直贯穿(从匹配案例开始),但使用条件禁用后续案例运行。

 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. } } 

跳到默认值

  • 使用默认情况下的空案例标签(名称明显)
  • 使用goto case "case-labelname"跳转到然后通过默认值

  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; }