【问题标题】:Jumping from one case to the default case in switch statement在 switch 语句中从一种情况跳转到默认情况
【发布时间】:2011-11-21 00:42:54
【问题描述】:
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 之外,还有其他方法吗?

【问题讨论】:

    标签: c switch-statement break


    【解决方案1】:

    好吧,帖子真的很旧,但要回答大家: 你可以简单地写'goto default;'你会直接跳转到默认情况下没有任何问题。

    例子:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        这就是我所做的:

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

        使用此结构,您可以从任何以前的案例切换到默认案例。对于打破外循环也很方便。

        【讨论】:

          【解决方案4】:

          重构你的代码:

          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: 处理程序添加到默认处理 switch 语句中。

          【讨论】:

          • 嗯...我忘记了这个问题。想知道为什么我没有收到@thetux4 的回复……(我也有点喜欢它,肯定不是太高级?;)
          【解决方案5】:

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

          【讨论】:

          • 箱内条件不一样。
          • 是的,但是您可以测试您想要“默认”情况的条件,并设置标志以触发 if 语句中“默认”代码的运行。 . 或者您是说您需要为每种情况使用不同的“默认”处理程序? ...我已经清理了一些代码以更好地解释我的意思。
          【解决方案6】:

          如果条件不依赖于案例,为什么要放在里面?

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

          【讨论】:

          • 我猜我问错了。我的意思是我应该在案件发生时检查情况。否则你是对的。
          • 你的意思是只有当 ch 属于 ('a', 'b', 'c', '-') 时才检查条件?
          【解决方案7】:

          我不确定这是否是最佳答案,但这里是:

          如果您绝对不想使用标签,并且希望将案例保持在当前顺序,那么您可以在案例 '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;
          }
          

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

          【讨论】:

          • 是的,但我猜不是使用 goto 这样做看起来更好吗?
          • 我同意。 goto 听起来确实是您的最佳选项。
          • 为什么不高效?无论如何,优化编译器都会找出组织代码的最佳方式。
          【解决方案8】:

          goto为了胜利

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

          【讨论】:

          • 但是如果你写意大利面条代码会被闪电击中的规则呢?
          • @KirkWoll:有效。我放了一个running example at ideone.com。 “chrome 开发者控制台”是否接受 C 代码?
          • @pmg,大声笑,非常抱歉。我用谷歌搜索了“javascript switch goto”,这是第二次点击。在我不专心的时候,我没有意识到这个问题与 Javascript 无关。
          • 如何在 javascript 中执行此操作?我相信 javascript 中没有 goto
          【解决方案9】:

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

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

          【讨论】:

          • 但是这一次情况 'b' 会有问题。
          • 啊,所有的情况都必须表现得一样吗?那么,你要多解释一下你的问题,因为例如条件取决于case的值?
          • 是的,但里面的条件不一定要根据具体情况。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-14
          相关资源
          最近更新 更多